Logger
public static object locker = new object();
public static LogConfiguration LogConfig
{
set;
get;
} static Logger()
{
if (LogConfig == null)
{
LogConfig = new LogConfiguration();
InitializeLog(ConfigurationManager.AppSettings["LogConfigPath"]);
} } public static void Debug(string message)
{
var configLevel = LogConfig.Level.ToUpper().Trim();
if (configLevel.Equals(LogLevel.DEBUG.ToString()))
{
WriteToFile(message, LogLevel.DEBUG);
} } public static void Warn(string message)
{
var configLevel = LogConfig.Level.ToUpper().Trim();
if (configLevel.Equals(LogLevel.ERROR.ToString()))
{
return;
}
else
{
WriteToFile(message, LogLevel.WARN);
}
} public static void Error(string message)
{
WriteToFile(message, LogLevel.ERROR);
} private static void WriteToFile(string message, LogLevel logLevel)
{
var logMessage = LogConfig.Format.Replace("%m", message);
logMessage = logMessage.Replace("%d", DateTime.Now.ToString(LogConfig.DateFormat.Message));
logMessage = logMessage.Replace("%p", logLevel.ToString());
logMessage = logMessage.Replace("%n", @"\r\n");
logMessage = logMessage.Replace("%c", ""); lock (locker)
{
try
{
using (StreamWriter writer = new StreamWriter(LogFileName, true, Encoding.Default))
{
writer.WriteLine(logMessage);
} }
catch (Exception ex)
{
string msg = string.Format("Write log failed.\r\n\r\n Error Info: {0}. \r\n\r\n Log Info: {1}", ex.ToString(), message);
WriteEventLog("NESO_LoggingComponent", msg, EventLogEntryType.Error);
}
}
} private static string LogFileName
{
get
{
var path = string.IsNullOrEmpty(LogConfig.Path) ? @"C:\" : LogConfig.Path; if (!path.EndsWith(@"\"))
{
path += "\\";
}
if (!Directory.Exists(LogConfig.Path))
{
Directory.CreateDirectory(LogConfig.Path);
}
var fileName = LogConfig.NameFormat; var logNameDateFormat = DateTime.Now.ToString(LogConfig.DateFormat.FileName); return string.Concat(path, fileName.Replace("%d", logNameDateFormat));
}
} #region Process Log config
private static void InitializeLog(string logConfigPath)
{
try
{
if (LogConfig == null)
{
LogConfig = new LogConfiguration();
}
if (string.IsNullOrEmpty(logConfigPath))
{
LogConfig.DateFormat = new DateFormat();
LogConfig.DateFormat.FileName = "yyyyMMdd";
LogConfig.DateFormat.Message = "yyyy-MM-dd HH:mm:ss";
LogConfig.Format = "[%p]%d:%m";
LogConfig.Level = LogLevel.ERROR.ToString();
LogConfig.NameFormat = "neso_log%d.txt";
if (string.IsNullOrEmpty(ConfigurationManager.AppSettings["LogPath"]))
{
LogConfig.Path = string.Concat(AppDomain.CurrentDomain.BaseDirectory, "log");
}
else
{
LogConfig.Path = ConfigurationManager.AppSettings["LogPath"];
}
}
else
{
string path = string.Concat(AppDomain.CurrentDomain.BaseDirectory.Trim('\\'), logConfigPath);
LogConfig = XmlHelper.ConvertToObject<LogConfiguration>(path); } }
catch (Exception e)
{
throw new Exception(string.Format("init log failed, {0}", e.Message));
}
}
#endregion
private static void WriteEventLog(string source, string content, EventLogEntryType type)
{
try
{
if (!EventLog.SourceExists(source))
{
EventLog.CreateEventSource(source, "Newegg NESO Log");
}
using (EventLog errorLog = new EventLog())
{
errorLog.Source = source;
errorLog.WriteEntry(content, type);
}
}
catch (Exception ex)
{
try
{
using (EventLog log = new EventLog("Application", ".", "Framework.Core"))
{
log.WriteEntry(ex.ToString(), EventLogEntryType.Error);
}
}
catch
{
}
}
}
} public enum LogLevel
{
DEBUG, WARN, ERROR
}
Logger的更多相关文章
- ABP源码分析八:Logger集成
ABP使用Castle日志记录工具,并且可以使用不同的日志类库,比如:Log4Net, NLog, Serilog... 等等.对于所有的日志类库,Castle提供了一个通用的接口来实现,我们可以很方 ...
- org.apache.log4j.Logger详解
org.apache.log4j.Logger 详解 1. 概述 1.1. 背景 在应用程序中添加日志记录总的来说基于三个目的 :监视代码中变量的变化情况,周期性的记录到文件中供其他应用进行统计分析工 ...
- Java程序日志:java.util.logging.Logger类
一.Logger 的级别 比log4j的级别详细,全部定义在java.util.logging.Level里面.各级别按降序排列如下:SEVERE(最高值)WARNINGINFOCONFIGFINEF ...
- [LeetCode] Logger Rate Limiter 记录速率限制器
Design a logger system that receive stream of messages along with its timestamps, each message shoul ...
- .Net Core Logger 实现log写入本地文件系统
.net core 自带一个基础的logger框架Microsoft.Extensions.Logging. 微软默认实现了Microsoft.Extensions.Logging.Console.d ...
- Android源码——Logger日志系统
Android的Logger日志系统是基于内核中的Logger日志驱动程序实现的. 日志保存在内核空间中 缓冲区保存日志 分类方法:日志的类型 + 日志的输出量 日志类型: main ...
- java.lang.NoClassDefFoundError: Lorg/slf4j/Logger;
如果你出现类似如下错误 1. Install tomcat7 in my home directory and set up `CATALINA_HOME` environment variable ...
- LeetCode 359 Logger Rate Limiter
Problem: Design a logger system that receive stream of messages along with its timestamps, each mess ...
- 你的日志组件记录够清晰嘛?--自己开发日志组件 Logger
现在现成的日志组件实在是太多太多,为什么我还需要自己实现呢????? 需求来源于java的log4j, [07-31 16:40:00:557:WARN : com.game.engine.threa ...
- log4j2 不使用配置文件,动态生成logger对象
大家平时使用Log4j一般都是在classpath下放置一个log4j的配置文件,比如log4j.xml,里面配置好Appenders和Loggers,但是前一阵想做某需求的时候,想要的效果是每一个任 ...
随机推荐
- [SignalR]SignalR与WCF双工模式结合实现服务端数据直推浏览器端
原文:[SignalR]SignalR与WCF双工模式结合实现服务端数据直推浏览器端 之前开发基于WinForm监控的软件,服务端基于Wcf实现,里面涉及双工模式,在客户端里面,采用心跳包机制保持与服 ...
- ENode 1.0 - Command Service设计思路
开源地址:https://github.com/tangxuehua/enode 上一篇文章,介绍了enode框架的物理部署思路.本文我们再简单分析一下Command Service的API设计: C ...
- Java多线程19:定时器Timer
前言 定时/计划功能在Java应用的各个领域都使用得非常多,比方说Web层面,可能一个项目要定时采集话单.定时更新某些缓存.定时清理一批不活跃用户等等.定时计划任务功能在Java中主要使用的就是Tim ...
- 《静静的dojo》 总体教程介绍
web2.0时代,ajax技术成为整个前端开发领域的基石.大部分的书籍.博客由此切入来介绍前端类库与框架,所以dojo往往只被当做一个ajax类库来介绍,然而仅仅以此来定位dojo,无异于管中窥豹.对 ...
- Golang下的Log处理
原创文章转载请注明出处:@协思, http://zeeman.cnblogs.com 后端系统中的Log是相当重要的,做过高并发服务的同学都会认同这一点.相对而言,调试已经用处不大了,对于这样的项目, ...
- HTML5打造的炫酷本地音乐播放器-喵喵Player
将之前捣腾的音乐频谱效果加上一个播放列表就成了现在的喵喵播放器(Meow meow Player,额知道这名字很二很装萌~),全HTML5打造的网页程序,可本地运行也可以挂服务器上用. 在线Demo及 ...
- Google 新推出Background sync API
Background sync是Google新推出的Web API,可延迟用户行为,直到用户网络连接稳定.这样有助于保证用户想要发送的数据就是实际发送的数据. 目前存在的问题 网络是消磨用户时间最多的 ...
- jQuery核心技术-----------------------------------------------------()
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Linux 容器技术史话:从 chroot 到未来
Linux 容器是一个在单一 Linux 主机上提供多个隔离的 Linux 环境的操作系统级虚拟技术.不像虚拟机(VM),容器并不需要运行专用的访客(guest)操作系统.容器们共享宿主机的(host ...
- javascript获取表单值的7种方式
见代码: <!doctype html> <html lang="en"> <head> <meta charset="UTF- ...