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,但是前一阵想做某需求的时候,想要的效果是每一个任 ...
随机推荐
- 作业七:团队项目——Alpha版本冲刺阶段-12
今日编写代码如下: class ChessRule { /**卒子的移动规则*/ public void armsRule(int Man,JLabel play,MouseEvent me){ // ...
- [.net 面向对象程序设计进阶] (3) 正则表达式 (二) 高级应用
[.net 面向对象程序设计进阶] (2) 正则表达式 (二) 高级应用 上一节我们说到了C#使用正则表达式的几种方法(Replace,Match,Matches,IsMatch,Split等),还 ...
- 更改Photoshop 语言为英语(无需语言包)
因为有时看国外教程时,手头上的PS是中文的而教程里的界面是英文的,而且中英菜单顺序在某些地方是不一样的,所以很不方便. 终于找到一个非常完美的方法可以把界面换成英文,而且不需任何语言包. 并且试了在最 ...
- Android消息机制
每一个Android应用在启动的时候都会创建一个线程,这个线程被称为主线程或者UI线程,Android应用的所有操作默认都会运行在这个线程中. 但是当我们想要进行数据请求,图片下载,或者其他耗时操作时 ...
- IOS Animation-CABasicAnimation例子(简单动画实现)
这些例子都是CABasicAnimation的一些简单实现的动画,例如移动.透明度.翻转等等.方法里面传入一个CALayer类或者子类就可以了. 下面是用swift实现的,这些方法我们也可以用作公共类 ...
- 知方可补不足~用CDC功能来对数据库变更进行捕捉
回到目录 如果我们希望监视一个数据表的变化,在sql2008之前的版本里,在数据库端可能想到的只有触发器,或者在程序端通过监视自己的insert,update,delete来实现相应的功能,这种实现无 ...
- PHP变量作用域
PHP 的全局变量和 C 语言有一点点不同,在 C 语言中,全局变量在函数中自动生效,除非被局部变量覆盖,这可能引起一些问题,有些人可能不小心就改变了一个全局变量.PHP 中全局变量在函数中使用时必须 ...
- Atitit 作用域的理解attilax总结
Atitit 作用域的理解attilax总结 1.1. 作用域是指对某一变量和方法具有访问权限的代码空间, 1 1.2. 作用域的使用提高了程序逻辑的局部性,增强程序的可靠性,减少名字冲突.1 1.3 ...
- atitit 验证码理论与概览与 验证码规范 解决方案.docx
atitit 验证码理论与概览与 验证码规范 解决方案.docx 1.1. 验证码的前世今生1 1.2. 第三代:无知识型验证码1 1.3. 短信验证码1 1.4. 语言验证码1 1.5. 图片验证 ...
- iOS-MVC模式
提到ios中的mvc不得不提2011秋季斯坦福课程的老头,他的iphone开发公开课是所有描述ios中mvc模式最为准确并且最为浅显易懂的. 模型-视图-控制器 这个模式其实应该叫做MCV,用控制器把 ...