From the Apache foundation

 

http://logging.apache.org/log4net/
log4net home (@ apache.org)

 

https://logging.apache.org/log4net/release/manual/introduction.html

 

FAQ

 

http://www.codeproject.com/Articles/19207/log4net-XmlConfigurator-Simplified

 

Log4Net with VB.Net - I mostly use C#, and the VB setup in 2.0 was throwing me off, a bit

 

 

Nothing is being logged. WTF?

log4net will not break your application if misconfigured. It just won’t output anything.

 

  1. Is log4net.config.XmlConfigurator.Configure(); executed somewhere?
  2. Make sure that the account running the application has write permissions to the target directory
  3. make sure that the correct levels are set in config
    1. If the levels are set at, say ERROR and the code only performs and INFO log
    2. the logfile will be created (if it does not exist)
    3. yet be mockingly empty. since it is below the target level.
    4. bwah-hah-hah-hah!

 

 

levels

  1. Debug
  2. Info
  3. Warn
  4. Error
  5. Fatal

 

 

Configuration

config examples from the official project

 

NOTE: your project may fail to compile if it is using the Client Profile.

 

 

 

 

notes on configuring path for log file

 

 

See http://stackoverflow.com/questions/2852923/class-decorator-to-declare-static-member-e-g-for-log4net for a static logger, and some xrefs to AOP usage. I’ve used the static class in a number of projects.

 

writing to the event log

  1. create an Event Source in the Windows Event Log
    1. run shell as administrator
    2. EVENTCREATE /ID 9999 /L APPLICATION /T INFORMATION /SO <appname> /d "first log"

 

 

<appender name="EventLogAppender" type="log4net.Appender.EventLogAppender" >
      <applicationName value="appname" />
      <filter type="log4net.Filter.LevelRangeFilter">
        <levelMin value="ERROR"/>
        <levelMax value="FATAL"/>
      </filter>
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
      </layout>
    </appender>

 

 

public static void Debug(object msg, int eventID) {
    LogEvent(msg, eventID, Level.Debug);
}

public static void Info(object msg, int eventID) {
    LogEvent(msg, eventID, Level.Info);
}

public static void Error(object msg, int eventID) {
    LogEvent(msg, eventID, Level.Error);
}

public static void LogEvent(object msg, int eventID, Level level) {

    try {

        var logData = new LoggingEventData();
        logData.Level = level;

        if (msg.GetType() == typeof(Exception)) {
            logData.ExceptionString = msg.ToString();
        } else {
            logData.Message = msg.ToString();
        }
        logData.TimeStamp = DateTime.Now;
        var loggingEvent = new LoggingEvent(logData);
        loggingEvent.Properties["EventID"] = eventID;

        GetLogger().Logger.Log(loggingEvent);

    } catch (Exception ex) {
        GetLogger().Error(ex);
    }
}

 

 

http://stackoverflow.com/questions/3412463/description-for-event-id-from-source-cannot-be-found
How to log EventID with EventLogAppender? (mailing list messages)
EventLog FUN (continued)
Log4Net Tutorial pt6 - LogEvent Context

 

I continued to have some issues w/ the event log appender. Here are some more links:

 

http://blog.sotec.eu/2011/11/log4net-using-eventlogappender-and.html
https://www.mail-archive.com/log4net-user@logging.apache.org/msg02397.html
http://apache-logging.6191.n7.nabble.com/How-to-set-the-category-and-EventId-in-EventLog-log4net-td22248.html
http://alsaydi.blogspot.com/2012/04/another-log4net-howto.html

 

 

 

 

 

Using in a static class

Based on code I saw at StackOverflow

 

NOTE: configurator must be called prior to class (application statup or somewhere).
Is there some lazy-load method of doing this?

 

NOTE: This is also an example of a web-client friendly error return (“An error has occured: Please contact support with case-number 925086ca-87ba-4a8d-8f82-09400d8a411d”) that logs a complete error/stack-trace to the log.
In practice, the word “support” could be replaced with something from the web.config -- it’s the parts clients want to change every couple of days until things shake out.
I saw this usage suggested somewhere, but I forget. It ensures you get a full log, but that the client gets friendly error-messages (of course, the message can be massaged further) without exposing implementation details that can be used as attack vectors.

 

 

See Also

Logging