12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using log4net.Core;
- using System.Reflection;
- namespace Aitex.DataAnalysis.Log
- {
- class LogWriter
- {
- private readonly static Type ThisDeclaringType = typeof(LogWriter);
- private readonly ILogger defaultLogger;
- public LogWriter()
- {
- defaultLogger = log4net.Core.LoggerManager.GetLogger(Assembly.GetExecutingAssembly(), "CommonLogger");
- }
- string FormatLogString(LogItem logItem)
- {
- string fileName = string.Empty;
- try
- {
- fileName = logItem.sf.GetFileName().Substring(logItem.sf.GetFileName().LastIndexOf('\\') + 1);
- }
- catch (Exception)
- {
- fileName = string.Empty;
- }
- //2014-10-17 17:53:51.9 INFO Log.cs Line:50 - Config模块初始化成功。
- string result = string.Format("{0} LOG.{1} {2} Line:{3} {4}() - {5}", logItem.dt.ToString("yyyy-MM-dd HH:mm:ss.fff"),
- logItem.lv.Name,
- fileName,
- logItem.sf.GetFileLineNumber().ToString(),
- logItem.sf.GetMethod().Name,
- logItem.msg);
- return result;
- }
- public string Write(LogItem logItem)
- {
- string log = FormatLogString(logItem);
- if (defaultLogger.IsEnabledFor(logItem.lv))
- {
- defaultLogger.Log(typeof(LogWriter), logItem.lv, log, logItem.ex);
- }
- return log;
- }
- }
- }
|