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. libj 0.8.2 发布,Java/JavaScript API 的 C++ 实现

    libj 0.8.2 增加了一些新的字符串相关的方法. libj 是一个跨平台的运行库,相当于提供了类似 Java/JavaScript API.libj 的内存管理是自动的,基于 shared_pt ...

  2. [.net 面向对象编程基础] (21) 委托

    [.net 面向对象编程基础] (20)  委托 上节在讲到LINQ的匿名方法中说到了委托,不过比较简单,没了解清楚没关系,这节中会详细说明委托. 1. 什么是委托? 学习委托,我想说,学会了就感觉简 ...

  3. 在MVVM模式中,按钮Click事件的绑定方法

    在MVVM模式中,我们将Button的方法写到ViewModel中,然后绑定到前端界面.通常的做法是写一个类,继承ICommand接口,然而如果按钮比较多的话,就需要写很多的类,对于后期维护造成很大的 ...

  4. [硬件项目] 2、汽车倒车雷达设计——基于专用倒车雷达芯片GM3101的设计方案与采用CX20106A红外线检测芯片方案对比

    前言 尽管每辆汽车都有后视镜,但不可避免地都存在一个后视镜的盲区,倒车雷达则可一定程度帮助驾驶员扫除视野死角和视线模糊的缺陷,提高驾驶安全性.上一节已经分析清倒车雷达的语音模块(上一节),本节将深入分 ...

  5. 使用后缀数组寻找最长公共子字符串JavaScript版

    后缀数组很久很久以前就出现了,具体的概念读者自行搜索,小菜仅略知一二,不便讨论. 本文通过寻找两个字符串的最长公共子字符串,演示了后缀数组的经典应用. 首先需要说明,小菜实现的这个后缀数组算法,并非标 ...

  6. HTML5打造的炫酷本地音乐播放器-喵喵Player

    将之前捣腾的音乐频谱效果加上一个播放列表就成了现在的喵喵播放器(Meow meow Player,额知道这名字很二很装萌~),全HTML5打造的网页程序,可本地运行也可以挂服务器上用. 在线Demo及 ...

  7. mysql C API的使用

    <MySQL++简介>介绍了如何使用C++来访问mysql,本文记录下使用C API访问mysql,mysql++就是对本文介绍的C-API的封装. 常用函数(名字就能告诉我们用法): M ...

  8. Node.js与Sails~方法拦截器policies

    回到目录 policies sails的方法拦截器类似于.net mvc里的Filter,即它可以作用在controller的action上,在服务器响应指定action之前,对这个action进行拦 ...

  9. EF架构~有时使用SQL更方便

    回到目录 在进行统计时,尤其是按月进行统计,由于我们采用的时间是一个2015-12-12日这种,所以在linq你无法进行拆分,你拆分了在发到SQL时也会报错,因为SQL那边更新不需要你.net的方法, ...

  10. 建站集成软件包 XAMPP搭建后台系统与微信小程序开发

    下载安装XAMPP软件,运行Apache和MySQL 查看项目文件放在哪个位置可以正常运行 然后访问localhost即可 下载weiphp官网的weiapp(专为微信小程序开发使用)放在htdocs ...