public static ILog GetLog(LogMediaEnum type) { XmlDocument xmldoc = new XmlDocument(); switch (type) { case LogMediaEnum.FILE: _xml.Append("<?xml version='1.0' encoding='utf-8' ?> <log4net> <logger name='Logger'> <appender-ref ref='FileAppender'/> <level value='info'/> </logger>"); _xml.Append("<appender name='FileAppender' type='log4net.Appender.RollingFileAppender'> <file value='logs/log' /> <appendToFile value='true' /> <rollingStyle value='Date' /> <datePattern value='_yyyy-MM-dd".log"' /> <layout type='log4net.Layout.PatternLayout'> <conversionPattern value='%newline记录时间:%date %newline 日志级别:%-5level %newline 出错类:%logger property:[%property{NDC}] 错误描述:%message%newline ' /> </layout> </appender>"); _xml.Append("</log4net>"); break; case LogMediaEnum.EMAIL: _xml.Append("<?xml version='1.0' encoding='utf-8' ?> <log4net> <logger name='Logger'> <appender-ref ref='SmtpAppender'/> <level value='info'/> </logger>"); _xml.Append("<appender name='SmtpAppender' type='log4net.Appender.SmtpAppender'> <authentication value='Basic' /> <smtpHost value='smtp.163.com' /> <from value='*****@*****.**' /> <to value='*****@*****.**' /> <username value='*****@*****.**' /><password value='like6522626' /> <bufferSize value='512' /> <subject value='Trace Logging' /><lossy value='true' /><evaluator type='log4net.Core.LevelEvaluator'><threshold value='ALL'/> </evaluator><layout type='log4net.Layout.PatternLayout'> <conversionPattern value='%newline记录时间:%date %newline 日志级别:%-5level %newline 出错类:%logger property:[%property{NDC}] 错误描述:%message%newline ' /> </layout> </appender>"); _xml.Append("</log4net>"); if (_email != string.Empty) { _xml.Replace("to value='*****@*****.**' ", string.Format("to value='{0}'", _email)); } break; case LogMediaEnum.DB: // <!-- DB--> http://www.poluoluo.com/jzxy/201311/249970.html break; default: break; } xmldoc.LoadXml(_xml.ToString()); XmlConfigurator.Configure(xmldoc.DocumentElement); return LogManager.GetLogger("Logger"); }
public static BaseLogger GetLogger(LogMediaEnum media) { switch (media) { case LogMediaEnum.FILE: return new FileLogger(); case LogMediaEnum.EMAIL: return new EmailLogger(); case LogMediaEnum.DB: return new DBLogger(); default: return null; } }
private void DoWrite(string message, LogMediaEnum[] mediaArray, object obj) { if (mediaArray == null || mediaArray.Count() == 0) mediaArray = new LogMediaEnum[] { LogMediaEnum.FILE }; try { foreach (var type in mediaArray) { var logger = LoggerFactory.GetLogger(type); if (logger == null) continue; ErrorLog = logger.Log; logger.Write(message, obj); } } catch (Exception ex) { if (ErrorLog != null) { this.ErrorLog.Error(string.Format("{0} inserts access log failed.", this.GetType().Name), ex); } } }
public void Write(string message, LogMediaEnum[] mediaArray, object obj) { ThreadPool.QueueUserWorkItem((a) => { DoWrite(message, mediaArray, obj); }); }
public void Write(string message, LogMediaEnum[] mediaArray) { Write(message, mediaArray, null); }