Tuesday, May 14, 2013

Enterprise Library 6.0: The LogWriter has not been set for the Logger static class. Set it invoking the Logger.SetLogWriter method

A common approach to create a log entry using the following code-
LogEntry entry = new LogEntry();
entry.Message = "I am logging";
Logger.Write(entry);
This works fine with Enterprise Library 5.0. But in 6.0 it gives the error in the title. We can solve this by the following-
Logger.SetLogWriter(new LogWriterFactory().Create());
LogEntry entry = new LogEntry();
entry.Message = "I am logging";
Logger.Write(entry);
Or we can also try-
IConfigurationSource configurationSource = ConfigurationSourceFactory.Create();
LogWriterFactory logWriterFactory = new LogWriterFactory(configurationSource);
Logger.SetLogWriter(logWriterFactory.Create());
LogEntry entry = new LogEntry();
entry.Message = "I am logging";
Logger.Write(entry);

5 comments:

  1. that's exactly i was looking for.
    thanks a lot!

    ReplyDelete
  2. log file is not creating with this code, and there is no errors and issues. can anyone help on this ,how to generate .log files with EntLib6.

    ReplyDelete
  3. Works fine for me.

    Raj:
    Ensure that your app.conf/web.config is configured correctly. If you're configuring problematically, check that.

    Mukul Sahu:
    Why don't you want to use it? It is a little confusing at first, but it does validation and does make the configuration easier.

    ReplyDelete
  4. Awesome...exactly what i was looking for....Thank You so much Anup !!

    ReplyDelete