rea*_*que 3 c# logging log4net
我在整个解决方案中使用log4net来满足我的日志需求(它有各种各样的项目,反过来有各种类).
是否真的需要始终创建一个实例(aka对象),如下所示:
private static readonly ILog log = LogManager.GetLogger(typeof(MyApp));
Run Code Online (Sandbox Code Playgroud)
或者我这样做的方式:
private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
Run Code Online (Sandbox Code Playgroud)
要使用log4net吗?是否有替代方案,以便我不必在课堂上这样做,因此需要更少的副本和粘贴:-)?
在一个小帮手的帮助下:
public static class LogGateway
{
static LogGateway()
{
XmlConfigurator.Configure();
}
public static ILog For( object LoggedObject )
{
if ( LoggedObject != null )
return For( LoggedObject.GetType() );
else
return For( (string)null );
}
public static ILog For( Type ObjectType )
{
if ( ObjectType != null )
return LogManager.GetLogger( ObjectType );
else
return LogManager.GetLogger( string.Empty );
}
public static ILog For( string LoggerName )
{
return LogManager.GetLogger( LoggerName );
}
}
Run Code Online (Sandbox Code Playgroud)
你没有复制代码,你只需将记录器称为:
...
LogGateway.For( this ).Debug( "hello!" ); // instance method
LogGateway.For( typeof( Foo ) ).Debug( "hello!" ); // static method
Run Code Online (Sandbox Code Playgroud)