views:

106

answers:

4

I have class

namespace LogToolsTest
{
    public class Foo
    {
        private static readonly ILog logger = LogManager.GetLogger(typeof(Foo));

        public Foo()
        {

            logger.Debug("Save this text");
            logger.Info("Save this text");
            logger.Warn("Save this text");
            logger.Error("Save this text");
            logger.Fatal("Save this text");


            var b1 = logger.IsDebugEnabled;
            var b2 = logger.IsInfoEnabled;
            var b3 = logger.IsWarnEnabled;
            var b4 = logger.IsErrorEnabled;
            var b5 = logger.IsFatalEnabled;
        }
    }
}

I test this by simple:

 Foo foo = new Foo();

and log4net.config

    <?xml version="1.0" encoding="utf-8" ?>
<log4net>
  <root>
    <level value="DEBUG" />
    <appender-ref ref="EventLogAppender" />
  </root>

  <appender name="EventLogAppender" type="log4net.Appender.EventLogAppender" >
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
    </layout>
  </appender>


</log4net>

I added to my assemblyinfo:

[assembly: log4net.Config.XmlConfigurator(ConfigFile = 
"App.config", Watch = true)]

After exection of this code there is no entry in eventlog. Why is that?

Now is worki almost ok: I deleted log4net.config, and modified my app.config

<appender name="TraceAppender" type="log4net.Appender.TraceAppender">
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern
       value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
  </layout>
</appender>

<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern
       value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
  </layout>
</appender>

<appender name="FileAppender" type="log4net.Appender.FileAppender">
  <file value="c:\\LOGS\\SampleLog.txt" />
  <appendToFile value="true" />
  <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern
       value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
  </layout>
</appender>
<appender name="EventLogAppender" type="log4net.Appender.EventLogAppender" >
  <layout type="log4net.Layout.PatternLayout">
  </layout>
</appender>

But something wrong I see in output:

System.Configuration.ConfigurationErrorsException: Configuration system failed to initialize ---> System.Configuration.ConfigurationErrorsException: Unrecognized configuration section log4net. (C:\toolkit\trunk\LogToolsTest\bin\Debug\LogToolsTest.vshost.exe.config line 3)
   at System.Configuration.ConfigurationSchemaErrors.ThrowIfErrors(Boolean ignoreLocal)
   at System.Configuration.BaseConfigurationRecord.ThrowIfParseErrors(ConfigurationSchemaErrors schemaErrors)
   at System.Configuration.BaseConfigurationRecord.ThrowIfInitErrors()
   at System.Configuration.ClientConfigurationSystem.EnsureInit(String configKey)
   --- End of inner exception stack trace ---
   at System.Configuration.ConfigurationManager.PrepareConfigSystem()
   at System.Configuration.ConfigurationManager.GetSection(String sectionName)
   at System.Configuration.PrivilegedConfigurationManager.GetSection(String sectionName)
   at System.Diagnostics.DiagnosticsConfiguration.GetConfigSection()
   at System.Diagnostics.DiagnosticsConfiguration.Initialize()
   at System.Diagnostics.DiagnosticsConfiguration.get_IndentSize()
   at System.Diagnostics.TraceInternal.InitializeSettings()
   at System.Diagnostics.TraceInternal.WriteLine(String message)
   at System.Diagnostics.Trace.WriteLine(String message)
   at log4net.Util.LogLog.EmitErrorLine(String message)
log4net:ERROR DefaultRepositorySelector: Exception while reading ConfigurationSettings. Check your .config file is well formed XML.
System.Configuration.ConfigurationErrorsException: Configuration system failed to initialize ---> System.Configuration.ConfigurationErrorsException: Unrecognized configuration section log4net. (C:\toolkit\trunk\LogToolsTest\bin\Debug\LogToolsTest.vshost.exe.config line 3)
   at System.Configuration.ConfigurationSchemaErrors.ThrowIfErrors(Boolean ignoreLocal)
   at System.Configuration.BaseConfigurationRecord.ThrowIfParseErrors(ConfigurationSchemaErrors schemaErrors)
   at System.Configuration.BaseConfigurationRecord.ThrowIfInitErrors()
   at System.Configuration.ClientConfigurationSystem.EnsureInit(String configKey)
   --- End of inner exception stack trace ---
   at System.Configuration.ConfigurationManager.PrepareConfigSystem()
   at System.Configuration.ConfigurationManager.GetSection(String sectionName)
   at System.Configuration.ConfigurationManager.get_AppSettings()
   at log4net.Util.SystemInfo.GetAppSetting(String key)

foreach appender.

+2  A: 

You are having permissions issues. I just took your code and with one minor tweak added a source I know my user has write access to (MSSQLSERVER)

<appender name="EventLogAppender" type="log4net.Appender.EventLogAppender">
   <param name="ApplicationName" value="MSSQLSERVER" />

Ran your program and it worked fine. You need to setup whatever sources you are going to use. Once you are done you will need to restart your computer...

My hint that something was wrong was there was a line that said

log4net: EventLogAppender: Source [TestApplication.vshost.exe] is registered to log []

I then changed it to "MSSQLSERVER"

log4net: EventLogAppender: Source [MSSQLSERVER] is registered to log [Application]

Some useful tips turn on debug:

 <log4net debug="true">

Or create an app.config with these settings:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="log4net.Internal.Debug" value="true" />
  </appSettings>
  <system.diagnostics>
    <trace autoflush="true">
      <listeners>
        <add
            name="textWriterTraceListener"
            type="System.Diagnostics.TextWriterTraceListener"
            initializeData="trace.txt" />
      </listeners>
    </trace>
  </system.diagnostics>

</configuration>
Nix
still nothing. EventLog has no new entries and all parameters of logger are set to false
phenevo configured log4net with an attribute. no need to call this method
Stefan Egli
A: 

You shouldn't need to call XmlConfigurator.Configure() because of the line you put in your assemblyinfo. However, if you have a bunch of assemblies, one important thing to keep in mind is that the FIRST project that contains a log call is the one whose assemblyinfo needs that line.

In other words, if you have a command line project and a dll project, and the command line project is doing new Foo() but Foo is defined in the dll project, it is the dll project that needs the line in its assembly info. But if you then add a log call in the command line project that says "I'm about to call Foo" your logging will stop working (because now the console project is the first one that makes a log call, and the config line needs to be in that assembly info!).

So the safest thing for fewest headaches is add that line to every assembly info you've got.

Eggplant Jeff
I removed XmlConfigurator.Configure() and added this section, and still the same :/
the configSections element for log4net is only required if you have the log4net config in the app.config file.
Stefan Egli
Sorry, missed that. However the rest of my answer still applies.
Eggplant Jeff
yes, that is why *I* did not downvote you
Stefan Egli
+2  A: 

I assume that you have a console program (it looks like you are testing log4net). So the first thing I would do is to make sure that logging works with more basic appenders. Configure a ConsoleAppender and make sure that you see the log statements. It is not strictly necessary to this, but as I said, that is what I would do.

The more important thing to do is to turn on internal debugging that should tell you why nothing is written to the event log.

Most likely you are facing a permission problem. Cf. log4net faq.

Stefan Egli
A: 

One thing I haven't seen mentioned is whether the Log4Net.config file is copying to your bin directory. I've run into this a few times and it has an easy fix.

In your solution explorer, right-click the Log4Net.config file and make sure the field for Copy to Output Directory is set to Copy Always or Copy if Newer (your preference). I tend to overlook that step when setting up a solution the first time.

Hope that helps you out.

SethO