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,但是前一阵想做某需求的时候,想要的效果是每一个任 ...
随机推荐
- 微信公共平台开发-(.net实现)3--发送文本消息
最近,项目这边比较忙,没来得及续写,哎,先吐吐槽吧,在这个周六还得来上班,以后每个周六多要上,一天的滋味真有点受不鸟呀.还不习惯ing... 嗯,别的不说了现在开始接着上次http://www.cnb ...
- 【Win10 UWP】QQ SDK(二):SDK的回调处理
上一讲,我们介绍了QQ SDK的使用方法,请看<[Win10 UWP]QQ SDK(一):SDK基本使用方法> 一. 回调的基本形式 从前面的介绍中我们知道,我们的应用和QQ客户端之间需要 ...
- jQuery Ready 与 Window onload 的区别(转)
“我们都知道,很多时候,在页面加载完后都需要做一些相应的初始化动作.例如,运行某些js特效,设置表单等等.怎么知道页面加载完了呢?一 般情况下都是设置body标签的onload监听window的loa ...
- 【C语言学习】《C Primer Plus》第13章 文件输入/输出
学习总结 1.文件函数原型1: FILE* fopen(char *filename, char *openmode); //打开文件,返回文件指针 filename:文件名,更确切地说,是包含文件 ...
- Entity Framework返回IEnumerable还是IQueryable?
在使用EF的过程中,我们常常使用repository模式,本文就在repository层的返回值是IEnumerable类型还是IQueryable进行探讨. 阅读目录: 一.什么是Repositor ...
- [.net 面向对象编程基础] (12) 面向对象三大特性——继承
[.net 面向对象编程基础] (12) 面向对象三大特性——继承 上节我们说了面向对象的三大特性之一的封装,解决了将对同一对象所能操作的所有信息放在一起,实现统一对外调用,实现了同一对象的复用,降低 ...
- Nginx + CGI/FastCGI + C/Cpp
接着上篇<Nginx安装与使用>,本篇介绍CGI/FASTCGI的原理.及如何使用C/C++编写简单的CGI/FastCGI,最后将CGI/FASTCGI部署到nginx.内容大纲如下: ...
- Unity3D热更新全书-脚本(二) 两级分化
上篇明确了我们探讨的脚本是什么:是写在文本文件里面的代码,可以作为资源加载,取得字符串再执行. 可是为什么世界上会有那么多的脚本?而其使用方法完全看起来不一样呢?这是因为每种脚本都有自己的定位,在不同 ...
- 手把手搭建WAMP+PHP+SVN开发环境
一:WAMP 这款软件在安装的过程中就已经把Apache.MySQL.PHP继承好了,而且也做好了相应的配置,除此之外,还加上了SQLitemanager和Phpmyadmin,省去了很多复杂的配置过 ...
- jetty
相关的文章太多了,我只按照自己的意思做简单总结. 参见: http://www.cnblogs.com/duanxz/p/3154982.html http://www.cnblogs.com/win ...