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的更多相关文章

  1. ABP源码分析八:Logger集成

    ABP使用Castle日志记录工具,并且可以使用不同的日志类库,比如:Log4Net, NLog, Serilog... 等等.对于所有的日志类库,Castle提供了一个通用的接口来实现,我们可以很方 ...

  2. org.apache.log4j.Logger详解

    org.apache.log4j.Logger 详解 1. 概述 1.1. 背景 在应用程序中添加日志记录总的来说基于三个目的 :监视代码中变量的变化情况,周期性的记录到文件中供其他应用进行统计分析工 ...

  3. Java程序日志:java.util.logging.Logger类

    一.Logger 的级别 比log4j的级别详细,全部定义在java.util.logging.Level里面.各级别按降序排列如下:SEVERE(最高值)WARNINGINFOCONFIGFINEF ...

  4. [LeetCode] Logger Rate Limiter 记录速率限制器

    Design a logger system that receive stream of messages along with its timestamps, each message shoul ...

  5. .Net Core Logger 实现log写入本地文件系统

    .net core 自带一个基础的logger框架Microsoft.Extensions.Logging. 微软默认实现了Microsoft.Extensions.Logging.Console.d ...

  6. Android源码——Logger日志系统

    Android的Logger日志系统是基于内核中的Logger日志驱动程序实现的. 日志保存在内核空间中 缓冲区保存日志   分类方法:日志的类型  +   日志的输出量   日志类型:   main ...

  7. java.lang.NoClassDefFoundError: Lorg/slf4j/Logger;

    如果你出现类似如下错误 1. Install tomcat7 in my home directory and set up `CATALINA_HOME` environment variable ...

  8. LeetCode 359 Logger Rate Limiter

    Problem: Design a logger system that receive stream of messages along with its timestamps, each mess ...

  9. 你的日志组件记录够清晰嘛?--自己开发日志组件 Logger

    现在现成的日志组件实在是太多太多,为什么我还需要自己实现呢????? 需求来源于java的log4j, [07-31 16:40:00:557:WARN : com.game.engine.threa ...

  10. log4j2 不使用配置文件,动态生成logger对象

    大家平时使用Log4j一般都是在classpath下放置一个log4j的配置文件,比如log4j.xml,里面配置好Appenders和Loggers,但是前一阵想做某需求的时候,想要的效果是每一个任 ...

随机推荐

  1. 使用JS或jQuery模拟鼠标点击a标签事件

    <a id="alink" href="abc.aspx"  style="visibility: hidden;">下一步&l ...

  2. web.xml详解

    web.xml的作用 web.xml,一个Tomcat工程中最重要的配置文件.web.xml没有其实也可以----只要你确定你的项目里面不需要任何过滤器.监听器.Servlet等等.我试了一下,没有w ...

  3. Java语法糖2:自动装箱和自动拆箱

    前言 一开始想学学自动拆箱和自动装箱是被这个名字吸引到,听上去好像很高端的样子,其实自动拆箱.自动装箱是很简单的内容. 自动拆箱和自动装箱 Java为每种基本数据类型都提供了对应的包装器类型.举个例子 ...

  4. 第二次作业:Github的使用

    Github的使用 一·注册:     要想使用github第一步当然是注册github账号了.之后就可以创建仓库了(免费用户只能建公共仓库),Create a New Repository,填好名称 ...

  5. 走进AngularJs(五)自定义指令----(下)

    自定义指令学习有段时间了,学了些纸上谈兵的东西,还没有真正的写个指令出来呢...所以,随着学习的接近尾声,本篇除了介绍剩余的几个参数外,还将动手结合使用各参数,写个真正能用的指令出来玩玩. 我们在自定 ...

  6. 实战使用Axure设计App,使用WebStorm开发(3) – 构建页面架构

    系列文章 实战使用Axure设计App,使用WebStorm开发(1) – 用Axure描述需求  实战使用Axure设计App,使用WebStorm开发(2) – 创建 Ionic 项目   实战使 ...

  7. Lucene系列-facet

    1.facet的直观认识 facet:面.切面.方面.个人理解就是维度,在满足query的前提下,观察结果在各维度上的分布(一个维度下各子类的数目). 如jd上搜“手机”,得到4009个商品.其中品牌 ...

  8. 上学时的HTML+JS+CSS(小总结)

    html:超文本标记语言 基本标签: { 文本标签:<pre></pre>:原封不动的保留空白区域.      <br />:换行.      <hr wid ...

  9. Java中反射的理解

    反射 一.什么是反射 Java 反射是Java语言的一个很重要的特征,它使得Java具体了"动态性". 反射用在 Java 身上指的是我们可以于运行时加载.探知.使用编译期间完全未 ...

  10. [全文检索]Lucene基础入门.

    本打算直接来学习Solr, 现在先把Lucene的只是捋一遍. 本文内容: 1. 搜索引擎的发展史 2. Lucene入门 3. Lucene的API详解 4. 索引调优 5. Lucene搜索结果排 ...