我不是标准I/O设施,而是仅使用log4net。 I configurelog4net, 以便同时使用ConsoleAppender和文档投标人或滚动装置对 con和记录仪。
The nice thing is that you can set the log message template to trap all sorts of useful information (time-of-day, thread/process id, machine name, etc.) in addition to just the logged message.
你们也可以向服务器、活动标志或重新激发思考。
容易!
这里的样本文本参考书,通过日志4net传递:
using System;
using System.IO;
using System.Text;
using log4net ;
namespace ConsoleApplication22
{
public class Log4NetTextWriter : TextWriter, IDisposable
{
private static ILog log = log4net.LogManager.GetLogger( typeof(Log4NetTextWriter) ) ;
#region properties
private StringBuilder buffer { get ; set ; }
public override Encoding Encoding
{
get
{
// since this TextWrite is writing to log4net, we have no idea what the final encoding might be.
// It all depends on the log4net configuration: tthe appender or appenders that wind up handling the logged message
// determine the final encoding.
//
// Might make more sense to return Encoding.UTF8 though, just to return something.
throw new NotImplementedException() ;
}
}
#endregion properties ;
public override void Flush()
{
if ( this.buffer != null && this.buffer.Length > 0 )
{
this.WriteLine() ;
}
return ;
}
public override void Close()
{
base.Close();
}
protected override void Dispose( bool disposing )
{
this.Flush() ;
base.Dispose( disposing );
}
#region public constructors
public Log4NetTextWriter() : this( null )
{
return ;
}
public Log4NetTextWriter( IFormatProvider formatProvider ) : base( formatProvider )
{
this.buffer = new StringBuilder() ;
}
#endregion public constructors
#region public Write() overloads
public override void Write( bool value )
{
this.buffer.Append( value ) ;
return ;
}
public override void Write( char value )
{
this.buffer.Append( value ) ;
return ;
}
public override void Write( char[] buffer )
{
this.buffer.Append( buffer ) ;
return ;
}
public override void Write( char[] buffer , int index , int count )
{
this.buffer.Append( buffer , index , count ) ;
return ;
}
public override void Write( decimal value )
{
this.buffer.Append( value ) ;
return ;
}
public override void Write( double value )
{
this.buffer.Append( value ) ;
return ;
}
public override void Write( float value )
{
this.buffer.Append( value ) ;
return ;
}
public override void Write( int value )
{
this.buffer.Append( value ) ;
return ;
}
public override void Write( long value )
{
this.buffer.Append( value ) ;
return ;
}
public override void Write(object value)
{
this.buffer.Append( value ) ;
return ;
}
public override void Write( string format , object arg0 )
{
this.buffer.AppendFormat( this.FormatProvider , format , arg0 ) ;
return ;
}
public override void Write( string format , object arg0 , object arg1 )
{
this.buffer.AppendFormat( this.FormatProvider , format , arg0 , arg1 ) ;
return ;
}
public override void Write( string format , object arg0 , object arg1 , object arg2 )
{
this.buffer.AppendFormat( this.FormatProvider , format , arg0 , arg1 , arg2 );
return ;
}
public override void Write( string format , params object[] arg )
{
this.buffer.AppendFormat( this.FormatProvider , format , arg ) ;
return ;
}
public override void Write( string value )
{
this.buffer.Append( value );
return ;
}
public override void Write( uint value )
{
this.buffer.Append( value );
return ;
}
public override void Write( ulong value )
{
this.buffer.Append( value );
return ;
}
public override void WriteLine()
{
string logMessage = this.buffer.ToString() ;
this.buffer.Length = 0 ;
log.Info( logMessage ) ;
return ;
}
#endregion public Write() overloads
#region public WriteLine() overloads
public override void WriteLine( bool value )
{
this.Write( value ) ;
this.WriteLine() ;
return ;
}
public override void WriteLine( char value )
{
this.Write( value ) ;
this.WriteLine() ;
return ;
}
public override void WriteLine( char[] buffer )
{
this.Write( buffer ) ;
this.WriteLine() ;
return ;
}
public override void WriteLine( char[] buffer , int index , int count )
{
this.Write( buffer , index , count ) ;
this.WriteLine() ;
return ;
}
public override void WriteLine( decimal value )
{
this.Write( value ) ;
this.WriteLine() ;
return ;
}
public override void WriteLine( double value )
{
this.Write( value ) ;
this.WriteLine() ;
return ;
}
public override void WriteLine( float value )
{
this.Write( value ) ;
this.WriteLine() ;
return ;
}
public override void WriteLine( int value )
{
this.Write( value ) ;
this.WriteLine() ;
return ;
}
public override void WriteLine( long value )
{
this.Write( value ) ;
this.WriteLine() ;
return ;
}
public override void WriteLine( object value )
{
this.Write( value ) ;
this.WriteLine() ;
return ;
}
public override void WriteLine( string format , object arg0 )
{
this.Write( format , arg0 ) ;
this.WriteLine() ;
return ;
}
public override void WriteLine( string format , object arg0 , object arg1 )
{
this.Write( format , arg0 , arg1 ) ;
this.WriteLine() ;
return ;
}
public override void WriteLine( string format , object arg0 , object arg1 , object arg2 )
{
this.Write( format , arg0 , arg1 , arg2 ) ;
this.WriteLine() ;
return ;
}
public override void WriteLine( string format , params object[] arg )
{
this.Write( format , arg ) ;
this.WriteLine() ;
return ;
}
public override void WriteLine( string value )
{
this.Write( value ) ;
this.WriteLine() ;
return ;
}
public override void WriteLine( uint value )
{
this.Write( value ) ;
this.WriteLine() ;
return ;
}
public override void WriteLine( ulong value )
{
this.Write( value ) ;
this.WriteLine() ;
return ;
}
#endregion public WriteLine() overloads
}
}
这里有1个样本日志。 这张标语既包括奥洛尔,也用于记录,根据大小进行自动滚动(也可以根据日期/时间或每秒进行滚动)。
<log4net>
<!-- Log to the console -->
<appender name="Console" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<!-- Pattern to output the caller s file name and line number -->
<conversionPattern value="%5level [%thread] (%file:%line) - %message%newline" />
</layout>
</appender>
<!-- Log to a log file. This particular setup should log to a static file name log.txt -->
<!-- When it hits 100KB in size, it rolls, keeping up to 10 archived files. The archived -->
<!-- files are named log.text.1 , log.txt.2 , ... , log.txt.10 -->
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="log.txt" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="100KB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="Console" />
<appender-ref ref="RollingFile" />
</root>
</log4net>
To configure log4net, the easiest way is by putting an XmlConfigurator attribute in AssemblyInfo.cs, thus:
// Configure log4net using the .log4net file
[assembly: log4net.Config.XmlConfigurator(ConfigFileExtension="log4net",Watch=true)]
// This will cause log4net to look for a configuration file
// called TestApp.exe.log4net in the application base
// directory (i.e. the directory containing TestApp.exe)
// The config file will be watched for changes.
虽然你的方案正在运行,但你可以通过编辑和保存集邮档案来扭转伐木或变造。 记录4net对编织文档的改动进行观察,并对飞行进行重新配置。