10

I've checked the connection string (I got it from the server explorer).

I've checked the commandText in log4net config.

I've checked the database permissions (integrated security is fine and works outside of the log4net class).

I've checked the repository's configured property (it is configured, it finds the config file fine).

I've also checked that the fields defined in the config file match the attributes (field size etc.) of the table in the database.

Any ideas?

When I'm debugging it seems to be hitting all the right methods at all the right times, with no exceptions raised.

<log4net>

  <appender name="ADONetAppender" type="log4net.Appender.ADONetAppender">
    <bufferSize value="1" />
    <connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
    <connectionString value="" />
    <commandText value="INSERT INTO dbo.Log4Net ([Date],[Thread],[Level],[Logger],[Message]) VALUES ('01-01-2001', 'test', 'test', 'test', 'test')"/>
    <!--<commandText value="INSERT INTO dbo.Log4Net ([Date],[Thread],[Level],[Logger],[Message],[Exception],[MachineName],[CultureId],[SourcePage],[Details],[Method]) VALUES (@log_date, @thread, @log_level, @logger, @message, @exception, @MachineName, @CultureId, @SourcePage, @Details, @Method)" />-->
    <parameter>
      <parameterName value="@log_date"/>
      <dbType value="DateTime"/>
      <layout type="log4net.Layout.RawTimeStampLayout"/>
    </parameter>
    <parameter>
      <parameterName value="@thread"/>
      <dbType value="String"/>
      <size value="255"/>
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%thread"/>
      </layout>
    </parameter>
    <parameter>
      <parameterName value="@log_level"/>
      <dbType value="String"/>
      <size value="50"/>
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%level"/>
      </layout>
    </parameter>

...more parameters

    <securitycontext type="log4net.Util.WindowsSecurityContext">
      <credentials value="Process">
      </credentials>
    </securitycontext>
  </appender>

  <appender name="FileAppender" type="log4net.Appender.FileAppender">
    <param name="File" value="LogTest.txt"/>
    <param name="AppendToFile" value="true"/>
    <layout type="log4net.Layout.PatternLayout">
      <param name="ConversionPattern" value="%d [%t] %-2p %c [%x] - %m%n"/>
    </layout>
  </appender>

  <root>
    <appender-ref ref="ADONetAppender"/>
    <appender-ref ref="FileAppender"/>
  </root>

</log4net>

It's writing to neither appender.

  • 2
    95% of the time you've messed something up on the config. Please post your log4net config. – Jeremy Holovacs Jun 13 '12 at 12:51
  • Thanks a lot for your response Jeremy, I'll prepare it for pasting here (removing conn strings etc.). Thanks. –  Jun 13 '12 at 12:52
  • 1
    add a file appender and check if it log on the file. If you can debug check if the flag IsInfoEnabled etc etc on the logger are true... – Felice Pollano Jun 13 '12 at 12:58
  • Thanks Felice - I tried the file appender too and have updated the OP. –  Jun 13 '12 at 13:33

7 Answers7

22

Right, after hours of pulling my hair out - I've cracked it.

This line:

log4net.Config.XmlConfigurator.Configure();

Needed putting in prior to any logging (well, as early as possible in the app). That's it. That was all it took. This is one of those problems were I'm extremely relieved but frustrated at the same time.

  • You'll need to write a new question I'm afraid. The problem you're having can't be the same as mine if my solution wasn't your solution. –  Jun 01 '15 at 08:58
12

I would recommend turning on Log4Net debugging:

<add key="log4net.Internal.Debug" value="true"/>

That may point you in the right direction if there's an error that's occurring behind the scenes. The output will be directed to the console output in the IDE or in the command line.

beaudetious
  • 2,354
  • 3
  • 36
  • 60
  • I've just tried this. I found no bugs but it still didn't work - and it **really** slowed down the web app. –  Jun 13 '12 at 15:16
  • 1
    It's just a troubleshooting step and certainly only used for debugging for a short time. I was hoping you'd find an error message or something this way. – beaudetious Jun 13 '12 at 16:09
  • 1
    It's a very useful step to be honest, I appreciate you posting it - definitely will be of use in the future! –  Jun 13 '12 at 16:24
  • Is there any way to get this information from software running in production? ~90% of the time my config works, but it'll miss ~10% of the logs – Liam Laverty Jul 14 '17 at 08:28
11
  1. Check if log4net.dll is placed in the same folder as your application.
  2. Try to enable log4net self-logging, maybe it'll help to find out:

    <configuration>
     <appSettings>
      <add key="log4net.Internal.Debug" value="true"/>
     </appSettings>
     <system.diagnostics>
      <trace autoflush="true">
       <listeners>
        <add name="textWriterTraceListener"
             type="System.Diagnostics.TextWriterTraceListener"
             initializeData="C:\tmp\log4net.txt" />
       </listeners>
      </trace>
     </system.diagnostics>
    </configuration>
    

See also the official log4net FAQ.

Lukas
  • 2,461
  • 2
  • 19
  • 32
Denis Kuzmin
  • 111
  • 2
3

I ran into a similar issue yesterday, Log4Net was just not writing to the database. I copied the configuration from another existing appender that successfully writes logs to the database. My solution was to run SQL Server Profiler to try and catch what was happening. Profiler showed that the INSERT statements were being sent by Log4Net, but it was failing on the SQL Server side. Manually running the INSERT statement in SQL Server Management Studio showed me exactly what was wrong with it, in my case, it was inserting NULL into a column that didn't accept NULL.

Dino Bansigan
  • 109
  • 2
  • 4
2

per the ADONetAppender config example:

<commandText value="INSERT INTO dbo.Log4Net 
    ([Date],[Thread],[Level],[Logger],[Message]) 
    VALUES (@log_date, @thread, @log_level, @logger, @message)"/>

This uses the ADO.NET parameterized query format, so you need to use that syntax. Additionally, you may not want to use integrated security for the db connection (esp. if you are running a web site or a service). For your file appender, I recommend a fully qualified path, and make sure it is writeable by the logger.

I assume you have already created the table in the specified database?

NOTE I recommend setting the trace appender in Debug mode too, to make sure you are actually logging stuff.

Jeremy Holovacs
  • 22,480
  • 33
  • 117
  • 254
  • Thanks a lot. Table is created yes. I will move away from integrated security once I can get a new set of db credentials set up but I've noted that thanks. I've changed the file appender's file property to a fully qualified one - still no log. By writeable - I'm assuming you mean permission to write to that disk? How do I check this? Thanks again. –  Jun 13 '12 at 15:15
  • well whatever security context you're running the assembly under will need write access to the folder. This should be NT-based file permissions. – Jeremy Holovacs Jun 13 '12 at 15:33
2

Add this line in the AssemblyInfo.cs file

[assembly: log4net.Config.XmlConfigurator(Watch = true)]

As the official configuration manual states "The log4net configuration can be configured using assembly-level attributes rather than specified programmatically", which I found as a more clear approach. Some people might find my answer as a more straightforward.

Source: https://logging.apache.org/log4net/release/manual/configuration.html

Dimitri
  • 141
  • 2
  • 4
  • 18
  • can you please elaborate and explain if this answers the question and how it improves a previously accepted answer ? – Gilles Gouaillardet Aug 29 '17 at 11:27
  • As the official configuration manual states "The log4net configuration can be configured using assembly-level attributes rather than specified programmatically", which I found as a more clear approach. Some people might find my answer as a more straightforward. Source: https://logging.apache.org/log4net/release/manual/configuration.html – Dimitri Aug 29 '17 at 17:29
  • 1
    thanks @Dimitri ! would you mind editing your answer instead of posting the explanations in a comment ? that will make things much easier for future readers – Gilles Gouaillardet Aug 30 '17 at 00:22
  • 1
    @GillesGouaillardet, done, thanks for your comments. – Dimitri Aug 30 '17 at 09:23
0

for sql server connectivity I'm using my live account to login to azure and sql server and due to that I had to change integrated security to "SSPI" instead of "true" did the trick

only found out it was the con string by adding this

<system.diagnostics>
<trace autoflush="true">
  <listeners>
    <add
        name="textWriterTraceListener"
        type="System.Diagnostics.TextWriterTraceListener"
        initializeData="C:\Logs\log4net.txt" />
  </listeners>
</trace>

Moi Hawk
  • 421
  • 1
  • 5
  • 12