/// <summary> /// 记录一条日志 /// </summary> /// <param name="entry">要记录的日志条目</param> public virtual void LogEntry( LogEntry entry ) { lock ( SyncRoot ) { if ( LogFilter == null || LogFilter.Writable( entry ) ) WriteLog( entry ); } }
protected override void WriteLogMessage( LogEntry entry, string[] contents ) { foreach ( var line in contents ) Writer.WriteLine( line ); if ( TextLogFileManager.AutoFlush ) Writer.Flush(); }
protected override void WriteLogMessage( LogEntry entry, string[] contents ) { lock ( _sync ) { foreach ( var line in contents ) Console.WriteLine( line ); } }
/// <summary> /// 重写 Writable 方法,确认日志条目是否由指定的日志源产生 /// </summary> /// <param name="entry">要检查的日志条目</param> /// <returns>日志条目是否由指定的日志源产生</returns> public override bool Writable( LogEntry entry ) { var source = entry.MetaData.LogSource(); if ( source == null ) return false; return string.Equals( SourceName, source.Name, StringComparison.OrdinalIgnoreCase ); }
/// <summary> /// 重写 WriteLogMessage 方法将日志写入文本文件 /// </summary> /// <param name="entry">日志条目对象</param> /// <param name="lines">要写入的文本行</param> protected override void WriteLogMessage( LogEntry entry, string[] lines ) { var path = GetFilepath( entry ); var build = new StringBuilder(); foreach ( var l in lines ) { build.Append( l ); build.Append( Environment.NewLine ); } TextLogFileManager.WriteText( path, build.ToString(), Encoding, entry.LogType().Serverity >= LogType.Error.Serverity ); }
/// <summary> /// 重写 WriteLogMessage 方法,在控制台输出日志信息 /// </summary> /// <param name="entry">日志条目</param> /// <param name="contents">要显示的文本内容</param> protected override void WriteLogMessage( LogEntry entry, string[] contents ) { lock ( _sync ) { var foreColor = Console.ForegroundColor; var backColor = Console.BackgroundColor; SetColor( entry ); Console.Write( string.Join( Environment.NewLine, contents ) ); Console.ForegroundColor = foreColor; Console.BackgroundColor = backColor; Console.WriteLine(); } }
private void SetColor( LogEntry entry ) { if ( entry.MetaData.LogType().Serverity <= LogType.Info.Serverity ) Console.ForegroundColor = ConsoleColor.Gray; else if ( entry.MetaData.LogType().Serverity <= LogType.ImportantInfo.Serverity ) Console.ForegroundColor = ConsoleColor.White; else if ( entry.MetaData.LogType().Serverity <= LogType.Warning.Serverity ) Console.ForegroundColor = ConsoleColor.Yellow; else if ( entry.MetaData.LogType().Serverity <= LogType.Error.Serverity ) Console.ForegroundColor = ConsoleColor.Red; else { Console.ForegroundColor = ConsoleColor.Yellow; Console.BackgroundColor = ConsoleColor.Red; } }
/// <summary> /// 对所有的日志记录器同时写入日志条目 /// </summary> /// <param name="entry">要记录的日志条目</param> protected override void WriteLog( LogEntry entry ) { List<Exception> exceptions = new List<Exception>(); foreach ( var logger in Loggers ) { try { logger.LogEntry( entry ); } catch ( Exception e ) { if ( ThrowExceptions ) exceptions.Add( e ); } } if ( exceptions.Any() ) throw new AggregateException( exceptions.ToArray() ); }
/// <summary> /// 重写 LogEntry 方法,交由内联日志记录器记录 /// </summary> /// <param name="entry">要记录的日志条目</param> public override void LogEntry( LogEntry entry ) { if ( InnerLogger == null ) throw new InvalidOperationException(); try { InnerLogger.LogEntry( entry ); } catch ( Exception e ) { try { if ( ExceptionLogger != null ) ExceptionLogger.LogException( e ); } catch { } throw; } }
/// <summary> /// 获取日志文件路径 /// </summary> /// <param name="entry">要记录的日志条目</param> /// <returns>日志文件路径</returns> protected override string GetFilepath( LogEntry entry ) { var path = _filenameProvider.GetName( entry ); return Path.Combine( basePath, path ); }
/// <summary> /// 重写 GetPadding 方法,返回空字符串,在控制台显示时取消填充 /// </summary> /// <param name="entry">当前要记录的日志条目</param> /// <returns>永远返回空字符串</returns> protected override string GetPadding( LogEntry entry ) { return ""; }
/// <summary> /// 由派生类实现,编写一条日志 /// </summary> /// <param name="entry">要编写的日志条目</param> public abstract void LogEntry( LogEntry entry );
public override bool Writable( LogEntry entry ) { return LogType.Equals( entry.LogType() ); }
/// <summary> /// 使用指定的前缀写入多行日志 /// </summary> /// <param name="padding">填充字符串,将会添加在每一行日志的前面</param> /// <param name="message">日志消息</param> protected virtual void Write( LogEntry entry, string padding, string message ) { var messageLines = SplitMultiLine( message ); if ( messageLines.Length == 1 ) { WriteLogMessage( entry, new[] { padding + " " + messageLines[0] } ); return; } for ( int i = 0; i < messageLines.Length; i++ ) { if ( i == 0 ) messageLines[i] = padding + "/" + messageLines[i]; else if ( i == messageLines.Length - 1 ) messageLines[i] = padding + "\\" + messageLines[i]; else messageLines[i] = padding + "|" + messageLines[i]; } WriteLogMessage( entry, messageLines ); }
public override string GetName( LogEntry entry ) { return _text; }
public override string GetName( LogEntry entry ) { return entry.LogDate.ToString( "yyyy-MM" ); }
/// <summary> /// 确定指定的日志条目是否需要记录 /// </summary> /// <param name="entry">日志条目</param> /// <returns>是否需要记录</returns> public abstract bool Writable( LogEntry entry );
public override void LogEntry( LogEntry entry ) { if ( Filter.Writable( entry ) ) InnerLogger.LogEntry( entry ); }
public override void LogEntry( LogEntry entry ) { entry = entry.SetMetaData( Source ); InnerLogger.LogEntry( entry ); }
/// <summary> /// 获取日志消息的填充,将会自动添加在日志消息的每一行的开头 /// </summary> /// <param name="entry">当前要记录的日志</param> /// <returns>当前日志消息的填充</returns> protected virtual string GetPadding( LogEntry entry ) { return GetTypePrefix( entry.MetaData.Type ) + " " + entry.LogDate.ToString( DateTimeFormatString ) + " "; }
/// <summary> /// 派生类实现此方法写入日志 /// </summary> /// <param name="contents">日志内容行</param> protected abstract void WriteLogMessage( LogEntry entry, string[] contents );
/// <summary> /// 写入一条日志信息 /// </summary> /// <param name="entry"></param> protected override void WriteLog( LogEntry entry ) { Write( entry, GetPadding( entry ), entry.Message ); }
protected abstract string GetFilepath( LogEntry entry );
public override bool Writable( LogEntry entry ) { return LogType.Equals( entry.MetaData.Type ); }
protected override void WriteLogMessage( LogEntry entry, string[] lines ) { var path = GetFilepath( entry ); Directory.CreateDirectory( Path.GetDirectoryName( path ) ); File.AppendAllLines( path, lines ); }
public override bool Writable( LogEntry entry ) { var serverity = entry.MetaData.Type.Serverity; return serverity >= MinServerity && serverity < MaxServerity; }
/// <summary> /// 由派生类实现,编写一条日志 /// </summary> /// <param name="entry">要编写的日志条目</param> protected abstract void WriteLog( LogEntry entry );
public override bool Writable( LogEntry entry ) { return Filters.Any( filter => filter.Writable( entry ) ); }
public override void LogEntry( LogEntry entry ) { try { _logger.LogEntry( entry ); } catch { } }
public override bool Writable( LogEntry entry ) { var serverity = entry.LogType().Serverity; return serverity >= MinServerity && serverity < MaxServerity; }