log4net waits until app pool recycle before logging to mssql2008 database - log4net

I'm configuring log4net with:
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "../ProjectName/Log4Net.config", Watch = true)]
I have 2 appenders configured:
1. An AdoNetAppender logging to a Log table on a mssql2008 database.
2. A FileAppender that just looks like this:
<appender name="FileAppender" type="log4net.Appender.FileAppender">
<file value="c:\log-file.txt" />
<appendToFile value="true" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
</layout>
</appender>
The problem is that when an action gets logged, it immediately appears in log-file.txt. It doesn't appear as an entry in the database until I either do iisreset, recompile the web code, or recycle the app pool.
The configuration seems right as the action does eventually get logged to the database. I just don't understand why the delay.
Can anybody give me a reason or tell me how to fix it?

The AdoNetAppender is a buffered appender. If you want it to write directly to the database you need to set the buffer size to 1:
<bufferSize value="1" />

Related

Create multiple logfiles during application run using log4net

I'm using log4net in a c# windows service, so the main application runs every time. I configured log4net to log into a date specific directory. Inside this directory log4net creates a logfile with a timestamp in the filename. This works fine. The probem is, that it only creates a new logfile, when the windows service restarts. This causes the logfile to get very big, expecially if the service runs over several days.
The question is:
Is there a possibility to force log4net to start with a new logfile throughout my code.
This is my log4net config:
<log4net>
<appender name="FileAppender" type="log4net.Appender.FileAppender">
<file type="log4net.Util.PatternString" value="${ProgramData}\Sirona\Log\AcqSrv\%date{yyMMdd}\AcqSrvAll-%date{HHmmss}.log" />
<appendToFile value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%2thread] [%-5level] [%logger] - %message%newline" />
</layout>
</appender>
<root>
<level value="ALL" />
<appender-ref ref="FileAppender" />
</root> <!-- Do not want NHibernate log -->
</log4net>
Best regards
Emu
I think your problem is solved by the RollingFileAppender: http://logging.apache.org/log4net/release/config-examples.html
http://logging.apache.org/log4net/release/sdk/log4net.Appender.RollingFileAppender.html
It works exactly as a FileAppender but once it reach a maximum size (configurable) it creates another log file.
You can also instruct it to keep only the latest X log files.
Hope it helps.

log4net doesn't watch my app.config

I configured my log4net to watch on changes made to the app.config file.
[assembly: log4net.Config.XmlConfigurator(Watch = true)]
When I run my app and change things in the config file, these changes only take effect when I restart my app. Why could this be?
Is there also a way to tell log4net to watch on changes in the app.config? Like:
<appender name="FileAppender" type="log4net.Appender.FileAppender" >
<watch value="true" />
</appender>
------------- EDIT -------------
I tried now to use a separate config-file: log4net.config.
It looks like this:
<log4net>
<appender name="FileAppender" type="log4net.Appender.FileAppender">
<file value="c:\log.txt" />
<appendToFile value="true" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%d [%t] %-5p %c (line %L) -- %m%n" />
</layout>
</appender>
<root>
<appender-ref ref="FileAppender" />
</root>
</log4net>
In my assemblyInfo.cs I wrote the following:
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]
The class that logs to the file looks like this:
ILog myLogger = LogManager.GetLogger(typeof(Form1));
myLogger.Debug("test");
This works like the old version. logfile entries are made, but when I change my log4net.config during runtime, these changes are not applied.... "Watch=true" should enable that feature, right?
HA!, I was just encountering the same problem, running unit tests that require logging.
Adding this line fixed it:
log4net.Config.XmlConfigurator.Configure();
My App.config:
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<log4net>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="log.txt" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="100KB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="RollingFileAppender" />
</root>
</log4net>
I also do have this:
[assembly: log4net.Config.XmlConfigurator(Watch = true)]
According to log4net documentation, the Watch feature does not work for application configuration files (app.config, web.config):
Because the System.Configuration API does not support reloading of the config file
the configuration settings cannot be watched using the
log4net.Config.XmlConfigurator.ConfigureAndWatch methods.
So if you need the log4net configuration to be re-configurable, you will need to place it in a separate XML file and your application needs to have sufficient permissions to read the file:
The file to read the configuration from can be specified using any of the log4net.Config.XmlConfigurator methods that accept a System.IO.FileInfo object. Because the file system can be monitored for file change notifications the ConfigureAndWatch methods can be used to monitor the configuration file for modifications and automatically reconfigure log4net.
Even though I'm terribly late to the party - here's, what helped me: a simple call to log4net.LogManager.GetLogger("DUMMY"); at the very beginning of my program. I put it in the very first line of program.cs's Main() method. No need to assign the logger to any object, merely a polite request to log4net to read the assembly's attributes as stated here.
Using attributes can be a clearer method for defining where the application's configuration will be loaded from. However it is worth noting that attributes are purely passive. They are information only. Therefore if you use configuration attributes you must invoke log4net to allow it to read the attributes. A simple call to LogManager.GetLogger will cause the attributes on the calling assembly to be read and processed. Therefore it is imperative to make a logging call as early as possible during the application start-up, and certainly before any external assemblies have been loaded and invoked.

Log4Net - Process Lock on Files

We're using Log4Net with a FileAppender to log errors during a process. As the process is finishing, we want to email the log file but we can't do that as it Log4Net still has a lock on the file and we get a message that the file is in use by another process. Any ideas?
We're using a config like this:
<appender name="WarnFileAppender" type="log4net.Appender.FileAppender">
<file value="d:\for-review-log.txt" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %logger - %message%newline" />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="INFO" />
<levelMax value="WARN" />
</filter>
<filter type="log4net.Filter.DenyAllFilter" />
<appendToFile value="false"></appendToFile>
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
</appender>
I assume log is from some kind of service, so the log will be locked until service is running.
You probably want to use shared lock while reading the file for sending.
Or you can copy the file and send the copy.
ive got the same problem. Olso a flush doesn't work and when i try to use the SMTPmailAppender that olso doesn't seem to work somehow. but when i use my own created class to send email it works, only he doesn't add the attachment(logfile)

Is it possible to write to an event log contained in a folder using log4net?

The goal is to have our application specific event logs in a folder with the name of our company. So, Company/App1, Company/App2. I would prefer to accomplish this with configuration, or with very little code. I tried setting logName to "MyFolder/MyLog" to no avail.
Example of a folder in event viewer, reference the Microsoft folder
This is my current Appender configuration:
<appender name="EventLogAppender" type="log4net.Appender.EventLogAppender">
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="WARN" />
<levelMax value="FATAL" />
</filter>
<applicationName value="ExampleApp" />
<logName value="MyLog" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>
You can set the path directly in the configuration, where you specify the log name, just specify the full path. You must however make sure that whatever user your application is running under has write access to that folder. Permissions are usually the cause for problems like this.

log4net does not log when my web app is packaged deployed

I have a web app that uses log4net to log errors to a log file.
It works well when I publish my website via xcopy but when I build a package installer, log4net does not appear to work when remote users access my site.
I use impersonate=true in my web.config and log4net only logs errors when I am the logged user.
I have another app that works fine in all cases but was not package deployed.
Does anyone have any thoughts?
After all it was a permission issue
setting the write permission to everyone log4net started logging well as it uses the
current logged user account
regards
i don't think so because same config is used in a working app
but here it is
what user does log4net uses when writing to disk?!
Is there any need to add permissions to write to log folders? Because i use impersonate=true the user logged to the site is the one that log4net is using to write
to file system right?!
<!-- log4net -->
<log4net debug="false">
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="D:\\WSBank\\Solution\\Logs\\Solution.log" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="1024KB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>
<root>
<level value="ALL" />
<appender-ref ref="RollingFileAppender" />
</root>
</log4net>

Resources