From xradiograph

Windows: Log 4 Net

From the Apache foundation
log4net home (@




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!




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




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 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"/>
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />



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;


    } catch (Exception ex) {
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:






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


Retrieved from
Page last modified on June 02, 2014, at 09:03 AM