我如何在自定义 xml 文件中定义 Log4Net 配置?
How can i define Log4Net configuration in a custom xml file?
我在我的 C# 应用程序中使用 Log4Net。我如何在自定义 xml 文件(而不是 appconfig 文件)中定义我的 Log4Net 配置?
我如何告诉我的 xml 配置程序使用那个特定的 xml 文件?
我想你的意思是这个:
log4net.Config.XmlConfigurator(配置文件=
"MyStandardLog4Net.config", 观看 = 真)]
参考:http://www.codeproject.com/Articles/140911/log-net-Tutorial
public 静态 class LogHelper
{
私有静态只读 ILog _log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
public static void InitLog()
{
string batchID = DateTime.Now.ToString("yyyy-MM-dd hh-mm-ss tt");
log4net.GlobalContext.Properties["LogName"] = batchID;
log4net.GlobalContext.Properties["ProjectName"] = Assembly.GetCallingAssembly().GetName().Name; // Get the Current Project Name
////log4net.Config.XmlConfigurator.Configure(); // Commented due to external log4net config file instead of App.config
//if (!String.IsNullOrEmpty(ConfigurationManager.AppSettings["Log4Net-ConfigFile"]) && !String.IsNullOrWhiteSpace(ConfigurationManager.AppSettings["Log4Net-ConfigFile"]))
//{
// log4net.Config.XmlConfigurator.ConfigureAndWatch(new System.IO.FileInfo(ConfigurationManager.AppSettings["Log4Net-ConfigFile"].Trim())); // External log4net.config file path
//}
log4net.Config.XmlConfigurator.Configure();
}
public static string CurrentLogFileName()
{
return Convert.ToString(log4net.GlobalContext.Properties["LogName"]);
}
public static void LogError(Exception ex)
{
_log.Error("System Error :" + ex.Message, ex);
}
public static void LogError(string message)
{
_log.Error("Custom Error :" + message);
}
public static void LogError(Exception ex, string message)
{
LogError(message);
LogError(ex);
}
public static void LogWarning(string message)
{
_log.Warn(message);
}
public static void LogMessage(string message)
{
_log.Info(message);
}
public static void LogDebug(string message)
{
_log.Debug(message);
}
}
<!-- Console Appender -->
<appender name="console" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %-5level: %message%newline" />
</layout>
</appender>
<!-- Colored ConsoleAppender -->
<appender name="ColoredConsoleAppender" type="log4net.Appender.ColoredConsoleAppender">
<mapping>
<level value="ERROR" />
<foreColor value="White, HighIntensity" />
<backColor value="Red, HighIntensity" />
</mapping>
<mapping>
<level value="Info" />
<foreColor value="Green, HighIntensity" />
<!--<backColor value="Green" />-->
</mapping>
<mapping>
<level value="Warn" />
<foreColor value="Yellow, HighIntensity" />
<!--<backColor value="Green" />-->
</mapping>
<mapping>
<level value="Debug" />
<foreColor value="Green, HighIntensity" />
<!--<backColor value="Green" />-->
</mapping>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %-5level: %message%newline" />
</layout>
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="RollingLogFileAppender" />
<appender-ref ref="ColoredConsoleAppender" />
</root>
我在我的 C# 应用程序中使用 Log4Net。我如何在自定义 xml 文件(而不是 appconfig 文件)中定义我的 Log4Net 配置? 我如何告诉我的 xml 配置程序使用那个特定的 xml 文件?
我想你的意思是这个: log4net.Config.XmlConfigurator(配置文件= "MyStandardLog4Net.config", 观看 = 真)]
参考:http://www.codeproject.com/Articles/140911/log-net-Tutorial
public 静态 class LogHelper { 私有静态只读 ILog _log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
public static void InitLog()
{
string batchID = DateTime.Now.ToString("yyyy-MM-dd hh-mm-ss tt");
log4net.GlobalContext.Properties["LogName"] = batchID;
log4net.GlobalContext.Properties["ProjectName"] = Assembly.GetCallingAssembly().GetName().Name; // Get the Current Project Name
////log4net.Config.XmlConfigurator.Configure(); // Commented due to external log4net config file instead of App.config
//if (!String.IsNullOrEmpty(ConfigurationManager.AppSettings["Log4Net-ConfigFile"]) && !String.IsNullOrWhiteSpace(ConfigurationManager.AppSettings["Log4Net-ConfigFile"]))
//{
// log4net.Config.XmlConfigurator.ConfigureAndWatch(new System.IO.FileInfo(ConfigurationManager.AppSettings["Log4Net-ConfigFile"].Trim())); // External log4net.config file path
//}
log4net.Config.XmlConfigurator.Configure();
}
public static string CurrentLogFileName()
{
return Convert.ToString(log4net.GlobalContext.Properties["LogName"]);
}
public static void LogError(Exception ex)
{
_log.Error("System Error :" + ex.Message, ex);
}
public static void LogError(string message)
{
_log.Error("Custom Error :" + message);
}
public static void LogError(Exception ex, string message)
{
LogError(message);
LogError(ex);
}
public static void LogWarning(string message)
{
_log.Warn(message);
}
public static void LogMessage(string message)
{
_log.Info(message);
}
public static void LogDebug(string message)
{
_log.Debug(message);
}
}
<!-- Console Appender -->
<appender name="console" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %-5level: %message%newline" />
</layout>
</appender>
<!-- Colored ConsoleAppender -->
<appender name="ColoredConsoleAppender" type="log4net.Appender.ColoredConsoleAppender">
<mapping>
<level value="ERROR" />
<foreColor value="White, HighIntensity" />
<backColor value="Red, HighIntensity" />
</mapping>
<mapping>
<level value="Info" />
<foreColor value="Green, HighIntensity" />
<!--<backColor value="Green" />-->
</mapping>
<mapping>
<level value="Warn" />
<foreColor value="Yellow, HighIntensity" />
<!--<backColor value="Green" />-->
</mapping>
<mapping>
<level value="Debug" />
<foreColor value="Green, HighIntensity" />
<!--<backColor value="Green" />-->
</mapping>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %-5level: %message%newline" />
</layout>
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="RollingLogFileAppender" />
<appender-ref ref="ColoredConsoleAppender" />
</root>