C# 避免在我要使用的每个类中获取记录器实例
如何避免在我想要使用日志记录的每个类上调用GetLogger?我更喜欢一些具有static属性的静态类,它根据调用者类类型初始化正确的记录器实例 当前配置 目前我有log4net配置文件,它与我需要登录的每个程序集一起加载。从AssemblyInfo.cs:C# 避免在我要使用的每个类中获取记录器实例,c#,logging,log4net,global-variables,C#,Logging,Log4net,Global Variables,如何避免在我想要使用日志记录的每个类上调用GetLogger?我更喜欢一些具有static属性的静态类,它根据调用者类类型初始化正确的记录器实例 当前配置 目前我有log4net配置文件,它与我需要登录的每个程序集一起加载。从AssemblyInfo.cs: [assembly: log4net.Config.XmlConfigurator( ConfigFile = xx.SharePoint.xxConfiguration.Log4NetConfigFilePath, Wat
[assembly: log4net.Config.XmlConfigurator(
ConfigFile = xx.SharePoint.xxConfiguration.Log4NetConfigFilePath,
Watch = true)]
然后在我的类中,我有一个初始化的iloglog
变量
ILog log = log4net.LogManager.GetLogger(this.GetType());
...
Log.Error(errorStr);
但我宁愿
Logging.Log.Error(errorStr);
通常在这种情况下使用单例设计模式,您可以发送 其他类就像一些Java库一样处理日志记录 您必须使用反射来确定调用方类的类型,我指的是通过以下方式调用它的类型:
Logging.Log.Error(errorStr);
希望对您有所帮助。在这种情况下通常使用单例设计模式,您可以发送 其他类就像一些Java库一样处理日志记录 您必须使用反射来确定调用方类的类型,我指的是通过以下方式调用它的类型:
Logging.Log.Error(errorStr);
希望对您有所帮助。您可以添加一个静态LogManager类,并将创建职责委托给它。然后它可以保存对ILog的引用,您可以根据需要使用它 我的班级是这样的:
public static class LogManager
{
public static readonly ILogger Log = CreateLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
/// <summary>
/// Creates the logger for the specified type.
/// </summary>
/// <param name="loggerType">Type of the logger.</param>
/// <returns></returns>
public static Logger CreateLogger(Type loggerType)
{
return new Logger(loggerType);
}
/// <summary>
/// Creates the logger for the specified name.
/// </summary>
/// <param name="loggerName">Name of the logger.</param>
/// <returns></returns>
public static Logger CreateLogger(string loggerName)
{
return new Logger(loggerName);
}
}
公共静态类日志管理器
{
public static readonly ILogger Log=CreateLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
///
///为指定类型创建记录器。
///
///记录器的类型。
///
公共静态记录器CreateLogger(类型loggerType)
{
返回新的记录器(loggerType);
}
///
///为指定的名称创建记录器。
///
///记录器的名称。
///
公共静态记录器CreateLogger(字符串loggerName)
{
返回新的记录器(loggerName);
}
}
您可以添加静态LogManager类,并将创建责任委托给它。然后它可以保存对ILog的引用,您可以根据需要使用它
我的班级是这样的:
public static class LogManager
{
public static readonly ILogger Log = CreateLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
/// <summary>
/// Creates the logger for the specified type.
/// </summary>
/// <param name="loggerType">Type of the logger.</param>
/// <returns></returns>
public static Logger CreateLogger(Type loggerType)
{
return new Logger(loggerType);
}
/// <summary>
/// Creates the logger for the specified name.
/// </summary>
/// <param name="loggerName">Name of the logger.</param>
/// <returns></returns>
public static Logger CreateLogger(string loggerName)
{
return new Logger(loggerName);
}
}
公共静态类日志管理器
{
public static readonly ILogger Log=CreateLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
///
///为指定类型创建记录器。
///
///记录器的类型。
///
公共静态记录器CreateLogger(类型loggerType)
{
返回新的记录器(loggerType);
}
///
///为指定的名称创建记录器。
///
///记录器的名称。
///
公共静态记录器CreateLogger(字符串loggerName)
{
返回新的记录器(loggerName);
}
}