public static void Write( string message, AppLogLevel level = AppLogLevel.Trace, Exception exception = null) { try { // メッセージの改行コードを置換する message = message.Replace(Environment.NewLine, "<br />"); message = message.Replace("\n", "<br />"); message = message.Replace("\r", "<br />"); defaultLogger?.Log(ConvetLogLevel(level), exception, message); Trace.WriteLine( $"{DateTime.Now:yyyy-MM-dd HH:mm:ss.fff} [{level.ToString().ToUpper().PadRight(5)}] {message}"); if (exception != null) { Trace.WriteLine(exception.ToFormat()); } } catch (Exception) { } }
/// <summary> /// Writes to logger. /// </summary> /// <param name="loglevel">The loglevel.</param> /// <param name="message">The message.</param> protected virtual void WriteToLogger(AppLogLevel loglevel, string message) { //used by the asf appender to log detailed or standard traces ThreadContext.Properties["LogLevel"] = loglevel.ToString(); foreach (var log in _loggers) { if (loglevel == AppLogLevel.Info && log.IsInfoEnabled) //informational messages { log.Info(message); } else if (loglevel == AppLogLevel.Warn && log.IsWarnEnabled) //warnings { log.Warn(message); } else if (loglevel == AppLogLevel.Error && log.IsErrorEnabled) //handled exceptions { log.Error(message); } else if (loglevel == AppLogLevel.Fatal && log.IsFatalEnabled) //un-handled exceptions, or dire exceptions { log.Fatal(message); } } ThreadContext.Properties["LogLevel"] = null; }
/// <summary> /// 获取储存文件名 /// </summary> /// <param name="logLevel"></param> /// <returns></returns> public static string GetStoreName(AppLogLevel logLevel) { if (!_storeNames.ContainsKey(logLevel)) { return(_storeNames[AppLogLevel.Info]); } return(_storeNames[logLevel]); }
/// <summary> /// Logs the exception as either a ERROR or FATAL depending upon log level. /// </summary> /// <param name="format">The format.</param> /// <param name="loglevel">The loglevel.</param> /// <param name="args">The arguments to format the message with.</param> public virtual void LogException(string format, AppLogLevel loglevel, params object[] args) { if (this.ParentLogger != null) { this.ParentLogger.LogException(format, loglevel, args); } var message = string.Format(format, args); this.WriteToLogger(loglevel, message); }
private static LogLevel ConvetLogLevel( AppLogLevel level) => new[] { LogLevel.Off, LogLevel.Trace, LogLevel.Debug, LogLevel.Info, LogLevel.Warn, LogLevel.Error, LogLevel.Fatal, }[(int)level];
/// <summary> /// Logs the exception as either a ERROR or FATAL depending upon log level. /// </summary> /// <param name="format">The format.</param> /// <param name="exception">The exception.</param> /// <param name="loglevel">The loglevel.</param> /// <param name="args">The arguments to format the message with.</param> public virtual void LogException(string format, Exception exception, AppLogLevel loglevel, params object[] args) { if (this.ParentLogger != null) { this.ParentLogger.LogException(format, exception, loglevel, args); } string exceptionMessage = null; if (exception != null) { exceptionMessage = exception.ToString(); } var message = string.Format(format, args); message += string.Format("\r\n{0}", exceptionMessage); this.LogException(message, loglevel); }
/// <summary> /// /// </summary> /// <param name="level">日志级别</param> /// <param name="method">方法名</param> /// <param name="tag">标签</param> /// <param name="msg">消息</param> /// <param name="userName">用户名</param> /// <param name="direction">Request/Reply</param> private static void WriteLog(AppLogLevel level, string method, string tag, string msg, string userName = null, string direction = null) { LogBase log = null; try { if (!_tokenBucket.TryGetToken(out log)) { log = new LogBase().SetContent(level, method, tag, msg, userName, direction); _monitor.WriteLocalLog(log); return; } log.SetContent(level, method, tag, msg, userName, direction); _monitor.WriteLog(log); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } }
/// <summary> /// 设置日志内容 /// </summary> /// <param name="level"></param> /// <param name="method"></param> /// <param name="tag"></param> /// <param name="msg"></param> /// <param name="userName"></param> /// <param name="direction"></param> /// <returns></returns> public LogBase SetContent(AppLogLevel level, string method, string tag, string msg, string userName, string direction) { this.LogTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"); this.LogLevel = level; this.Method = method; this.Tag = tag; this.Message = msg; this.UserName = userName; this.Direction = direction; try { this.ChainId = (CallContext.LogicalGetData("Rpc.ChainId")?.ToString()); this.TraceId = (CallContext.LogicalGetData("Rpc.TrackId")?.ToString()); this.ParentTraceId = (CallContext.LogicalGetData("Rpc.ParentTrackId")?.ToString()); } catch { //no code } return(this); }
public void SetAppLogLevel(AppLogLevel level) { //this controls what logging message should be set to AppCenter at all _logLevel = level; }