如何在自定义xml文件中定义Log4Net配置?

时间:2016-08-11 10:42:37

标签: c# log4net app-config log4net-configuration log4net-appender

我在C#中的应用程序中使用Log4Net。如何在自定义xml文件(而不是appconfig文件)中定义Log4Net配置? 如何告诉我的xmlconfigurator使用该特定的xml文件?

2 个答案:

答案 0 :(得分:0)

我认为你的意思是这个: log4net.Config.XmlConfigurator(ConfigFile =                 “MyStandardLog4Net.config”,Watch = true)]

参考:http://www.codeproject.com/Articles/140911/log-net-Tutorial

答案 1 :(得分:0)

公共静态类 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>