0

I'm using Log4Net in my application in C#. How can i define my Log4Net Configuration in a custom xml file (not the appconfig file) ? How can i tell my xmlconfigurator to use that specific xml file ?

patrickrv
  • 3
  • 2
  • Like this? ```log4net.Config.XmlConfigurator.Configure(new FileInfo("filename"));``` – tym32167 Aug 11 '16 at 10:50
  • Thanks, exactly what i was looking for : [assembly: XmlConfigurator(ConfigFile = "../../Configuration.xml", Watch = true)] – patrickrv Aug 11 '16 at 11:13

2 Answers2

0

I think you mean this one: log4net.Config.XmlConfigurator(ConfigFile = "MyStandardLog4Net.config", Watch = true)]

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

Jeroen Doppenberg
  • 1,558
  • 1
  • 10
  • 13
  • yes, just what i was looking for: [assembly: XmlConfigurator(ConfigFile = "../../Configuration.xml", Watch = true)] – patrickrv Aug 11 '16 at 11:14
0

public static class LogHelper { private static readonly 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>
  • Please don't post only code as an answer, but also provide an explanation of what your code does and how it solves the problem of the question. Answers with an explanation are usually more helpful and of better quality, and are more likely to attract upvotes. – Ran Marciano Feb 12 '21 at 08:35