Why do I get an empty event log when I do this with log4net?

I have a 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 check it 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 collection:

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

After executing this code, there is no entry in the event log. Why is this?

Now it works almost fine: I deleted log4net.config and changed my app.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <log4net>
  <root>
    <priority value="ALL" />
    <appender-ref ref="TraceAppender" />
    <appender-ref ref="ConsoleAppender" />
    <appender-ref ref="FileAppender" />
    <appender-ref ref="EventLogAppender" />
  </root>

  <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>
</log4net>
<configSections>
  <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
</configSections>

</configuration>

But something is wrong. I see in the 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.

+3
source share
4 answers

, (, log4net). , , , . ConsoleAppender , . , , , , .

- , , .

, . log4net faq.

+3

. , , (MSSQLSERVER)

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

, . , . , ...

, - , ,

log4net: EventLogAppender: [TestApplication.vshost.exe] []

"MSSQLSERVER"

log4net: EventLogAppender: [MSSQLSERVER] []

:

 <log4net debug="true">

app.config :

<?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>
+2

XmlConfigurator.Configure() - , . , , , FIRST, , , .

, dll, Foo(), Foo dll, DLL . , : " Foo", ( - , , !).

, .

0
source

One thing that I did not mention is copying the Log4Net.config file to the bin directory. I have come across this several times and it has an easy fix.

In your solution explorer, right-click the Log4Net.config file and verify that the field is Copy to Output Directoryset to Copy Alwaysor Copy if Newer(your preference). I try to ignore this step when setting up the solution for the first time.

Hope this helps you.

0
source

Source: https://habr.com/ru/post/1759317/


All Articles