Log4Net does not write to the database

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

I checked commandText in the log4net configuration.

I checked database access permissions (integrated protection is great and works outside of the log4net class).

I checked the configured repository (it is configured, it finds the configuration file perfectly).

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

Any ideas?

When I debug, it seems to click on all the correct methods at any time, with no exceptions.

<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> 

He writes to none of the applications.

+9
c # database sql-server logging log4net
source share
7 answers

That's right, after hours of stretching my hair - I cracked it.

This line:

 log4net.Config.XmlConfigurator.Configure(); 

Must be entered before registration (well, as early as possible in the application). It. That is all that is needed. This is one of those problems when I was very happy, but disappointed at the same time.

+18
source share

I would recommend enabling Log4Net debugging:

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

This may point you in the right direction if there is a mistake that happens behind the scenes. The output will be directed to console output in the IDE or on the command line.

+10
source share
  1. Check if log4net.dll is in the same folder as your application.
  2. Try enabling log4net self-registration, maybe this will help figure 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 .

+9
source share

behind the ADONetAppender configuration 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 request format with parameters, so you need to use this syntax. In addition, you can not use the built-in protection for the db connection (especially if you use a website or service). For your file application, I recommend the full path and make sure that it is recorded by the registrar.

Suppose you have already created a table in the specified database?

NOTE I recommend installing the monitor appender in debug mode to make sure that you are actually posting material.

+2
source share

Yesterday I ran into a similar problem, Log4Net just did not write to the database. I copied the configuration from another existing application that successfully writes logs to the database. My solution was to run SQL Server Profiler to try and catch what was going on. The profiler showed that the INSERT statements were sent by Log4Net, but it was unsuccessful on the SQL Server side. Manually running the INSERT statement in SQL Server Management Studio showed me what was wrong with her, in my case she inserted NULL into a column that did not accept NULL.

+2
source share

Add this line to the AssemblyInfo.cs file

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

The official configuration guide states: "The configuration of log4net can be configured using attributes at the assembly level, and not programmatically," which I found more understandable. Some people may find my answer as simpler.

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

+1
source share

to connect to the sql server, I use my real account to log in to the Azure server and SQL, and because of this I had to change the built-in protection to "SSPI" instead of "true".

only found out that it was a string against by adding this

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

0
source share

All Articles