Using the below configuration, about half of the time, I'm getting two logs per one, one empty. They differ in filename by one second (log.2015-03-09_11-50-25 vs log.2015-03-09_11-50-26)
I'm trying to have one log per run of the console application.
<log4net>
<appender name="Log" type="log4net.Appender.FileAppender">
<file type="log4net.Util.PatternString" value="C:\env\QA\Logs\consoleapp\log.%date{yyyy-MM-dd_HH-mm-ss}.txt"/>
<appendToFile value="true"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %message%newline"/>
</layout>
</appender>
<root>
<level value="INFO"/>
<appender-ref ref="Log"/>
</root>
</log4net>
The latter log is the only one that's populated/written to.
Why is this happening? How do I fix it?
EDIT: Turns out i was instantiating a second logger in my code. When the instantiation occurred during a different second, a second log was created. The appender was working correctly.
I've seen something similar before - it's due to the second being specified in the filename, and the writes are occuring during the end of one second and the start of another.
<file type="log4net.Util.PatternString" value="C:\env\QA\Logs\consoleapp\log.%date{yyyy-MM-dd_HH-mm-ss}.txt"/>
should be
<file type="log4net.Util.PatternString" value="C:\env\QA\Logs\consoleapp\log.%date{yyyy-MM-dd}.txt"/>
In my last job, somebody set the second specifier in the log file name, and the production server came close to crashing trying the render the 150,000 files contained in the log folder.
Edit:
If you want to write once per run, you can add the following to your log4net configuration:
<rollingStyle value="Once" />
and you might need to set the appendToFile attribute to be false.
log4net one file per run
so your config would look like this:
<appender name="Log" type="log4net.Appender.FileAppender">
<file type="log4net.Util.PatternString" value="C:\env\QA\Logs\consoleapp\log.%date{yyyy-MM-dd_HH-mm-ss}.txt"/>
<appendToFile value="false" />
<maxSizeRollBackups value="-1" /> <!--infinite-->
<rollingStyle value="Once" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %message%newline"/>
</layout>
</appender>
I used user1666620's solution, but still got a .log.1 file.
Turns out I had the following line in my AssemblyInfo.cs file:
[assembly: log4net.Config.XmlConfigurator(Watch = true)]
Removing the above line fixed my issue. By the way, my program.cs looks like:
public static class Program
{
static Program()
{
// Initialize logging
log4net.Config.XmlConfigurator.Configure();
}
private static readonly ILog log =
LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
...
}
Related
I am currently developing series of web services in WCF using .NET 4.5. For my logging I have chosen log4net framework but I found that its a big problem to use it in my project design.
I have everything separated into projects like this :
DataAccess
Common
etc. etc.
ExternalServicesContracts
ExternalServices
ExternalServicesProxies
InternalService
All of the services projects contains more services.
Situation
I need my log files to look like this ServiceName_YYYY_DDMM.log.
So in the log4net config file i need to use different loggers per namespace of the web service. I can achieve it by using loggers like this :
For the first service :
private static readonly ILog Log = LogManager.GetLogger(typeof(MyFirstService));
And the same for the second service :
private static readonly ILog Log = LogManager.GetLogger(typeof(MySecondService));
and then in config file I just point to them by loggers
<logger name="ExternalServices.MyFirstService">
<appender-ref ref="FirstServiceAppender"/>
</logger>
<logger name="ExternalServices.MySecondService">
<appender-ref ref="SecondServiceAppender"/>
</logger>
And in appenders
<appender type="log4net.Appender.RollingFileAppender" name="FirstServiceAppender">
<file value="c:\temp\FirstService"/>
<rollingStyle value="Composite" />
<datePattern value="_yyyy_MM_dd.lo\g"/>
<maxSizeRollBackups value="-1"/>
<maximumFileSize value="100MB"/>
....
</appender>
<appender type="log4net.Appender.RollingFileAppender" name="SecondServiceAppender">
<file value="c:\temp\SecondService"/>
<rollingStyle value="Composite" />
<datePattern value="_yyyy_MM_dd.lo\g"/>
<maxSizeRollBackups value="-1"/>
<maximumFileSize value="100MB"/>
....
</appender>
Problem
After this change, I can`t see anything from my DataAccess and Common because I had to do the same strategy with classes inside the layer.
So the logger will be loaded the same way like this:
private static readonly ILog Log = LogManager.GetLogger(typeof(UsersProvider));
I could add another logger for both projects but I need the logs from it to be written inside the same file (the log file used by the caller).
Question
I really cant separate the services into more projects because of the client.
Is there a way to use the same logger inside a library as the caller is using ? (So the DataAccess and Common writes logs into FirstServiceAppender when the service is using it.)
Or is there any pattern that would get me out of this situation ?
I don't have time to try this out right now, but this should be easier that you think it is. You can try configuring log4net to have a dynamic file name based on a GlobalContext.Properties value. In each service, put a value GlobalContext.Properties that "names" the service. This should be ok as each service is essentially a separate process, so using the GlobalContext should not result in any conflicts. This way you will need only one file target and your loggers can be configured very simply (just send the output to the one file target).
Configuration should be something like this:
<appender type="log4net.Appender.RollingFileAppender" name="RollingFileAppender">
<file type="log4net.Util.PatternString" value="C:\temp\%property{Service}"/>
<rollingStyle value="Composite" />
<datePattern value="_yyyy_MM_dd.log"/>
<maxSizeRollBackups value="-1"/>
<maximumFileSize value="100MB"/>
....
</appender>
<root>
<appender-ref ref="RollingFileAppender" />
</root>
In your code, when each service is initialized, put something like this:
GlobalContext.Properties["Service"] = "FirstService"; // for FirstService
GlobalContext.Properties["Service"] = "SecondService"; // for SecondService
Note that you must set the value into GlobalContext.Properties BEFORE anything else is done with log4net. The best place would be in the static constructor of your application/service.
This configuration should all logs generated in "FirstService" to be written to the FirstService log file and all logs generated in "SecondService" to be written to the SecondService log.
Update:
<appender name="DynamicRollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file type="log4net.Util.PatternString" value="c:\temp\%property{Service}" />
<datePattern value="_yyyy_MM_dd.lo\g" />
<appendToFile value="true" />
<maxSizeRollBackups value="-1" />
<maximumFileSize value="5000KB" />
<preserveLogFileNameExtension value="true"/>
<staticLogFileName value="false" />
<countDirection value="1"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%method] - %message%newline" />
</layout>
</appender>
You can pass logger as a parameter for classes. So maybe your DataAccess classes should take a logger as a constructor parameter and use it for loggin. Then you could give a desired logger from outside.
I am trying to configure log4net for the first time, I'm certain that I have everything configured properly, however, after getting empty log files I turned on the log4net debugger. I am now continually seeing the following error:
log4net:ERROR [RollingFileAppender] ErrorCode: GenericFailure. Unable to acquire lock on
file "file path\file name" The process cannot access the file "file path\file name" because
it is being used by another process.
I currently have log4net configured through my Web.config file thusly:
<log4net debug="true">
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="Logs\\TransferPicturesApplicationLog.txt"/>
<appendToFile value="true"/>
<rollingStyle value="Size"/>
<maxSizeRollBackups value="5"/>
<maximumFileSize value="10MB"/>
<staticLogFileName value="true"/>
<filter type="log4net.Filter.LevelRangeFilter">
<param name="LevelMin" value="ERROR"/>
<param name="LevelMax" value="DEBUG"/>
</filter>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %level %logger - %message%newline"/>
</layout>
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
</appender>
<root>
<level value="INFO"/>
<appender-ref ref="RollingFileAppender"/>
</root>
<logger>
<level value="DEBUG"/>
<appender-ref ref="RollingFileAppender"/>
</logger>
</log4net>
Help!!!
I also got this error because I specified the name of a directory as opposed to a file in the appender, the error message is cryptic:
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<lockingModel type="log4net.Appender.FileAppender+MinimalLock"/>
<file value="C:\DirectoryNameHere\MyBad" />
My guess would be that you have multiple copies of the appender across different assemblies trying to access this path, so they all attempt to take hold of the appropriate log file at the same time. However, to be sure, I would recommend you use Unlocker to make sure your file isn't being held by something else you don't expect. If you are having multiple instances of this appender trying to open the same file, your problem would be that you are not using the appender correctly; if the appender is being used across assemblies then the two invocations of it will not play nicely with one another, leading to an already in use error like this. If this is the case, refactoring is likely your only option.
I got this problem when I tried to write the logfile in a subdirectory (same as #PeteN). When using the correct syntax this doens't occur anymore, in de appender config set:
<appender name="TestLogAppender" type="log4net.Appender.RollingFileAppender">
<file type="log4net.Util.PatternString" value=".\logDirectory\LogFileName.csv" />
<....>
hope this helps
I'm trying to setup Log4Net (this is my first time using Log4Net) to log to a text file in an assembly. I'm not getting any errors, but it's also not working. I can breakpoint the lines where I am logging my output and see that they are reached, but like I say nothing happens.
Where am I going wrong?
I have added the following to my packages.config file, inside the <packages> attribute:
<log4net>
<appender name="FileAppender" type="log4net.Appender.FileAppender,log4net">
<file value="c:\CTI\log.txt" />
<appendToFile value="true" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %level %logger - %message%newline" />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="INFO" />
<levelMax value="FATAL" />
</filter>
</appender>
<root>
<level value="DEBUG"/>
<appender-ref ref="FileAppender"/>
</root>
</log4net>
</configuration>
I have added the following line to AssemblyInfo.cs:
[assembly: log4net.Config.XmlConfigurator(Watch=true)]
I added the Log4Net assembly using NuGet and I am logging like this:
private log4net.ILog _Log;
_Log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
_Log.Debug("FooBar");
Like I say, no errors but nothing happens either.
What am I missing?
One thing that is wrong is that you are adding the log4net configuration section to the nuget config file (packages.config).
You can have the configuration in the app/web config or in a separate file to which you point from the appSettings, e.g.
configuration is in a file called config.log4net (the copy to output directory attribute of the file is set to copy always) and you add the following entry in app/web.config:
<add key="log4net.config" value="config.log4net"/>
If you don't want to depend on a web/app configuration, you can set the ConfigFileExtension property of the XmlConfiguratorAttribute attribute in AssemblyInfo.cs:
[assembly: log4net.Config.XmlConfigurator(ConfigFileExtension = "log4net", Watch = true)]
Then name the log4net configuration file the same as your exe/assembly plus the configured extension, e.g. MyApplication.exe.log4net or MyLibrary.dll.log4net
Another thing that is wrong is your appender filter. The range that you have set excludes DEBUG level, which you expect to log. Here are all logging levels:
ALL
DEBUG
INFO
WARN
ERROR
FATAL
OFF
As you can see, DEBUG is not between INFO and FATAL.
In a project, i'm using log4net to write down in the event log. I need to create a custom log so all will go there. I have tried to add the event to different logs, and it worked, but for some reasons, now it is impossible to change the logname from the config file. Even if I change it, it will keep the previous one and log into it. Here is a sample of my app.config:
<appender name="EventLogAppender" type="log4net.Appender.EventLogAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date{yyyy-MMMM-dd HH:mm:ss, fff} [%thread] %level %message %exception%newline"/>
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="DEBUG" />
<levelMax value="FATAL" />
</filter>
<ApplicationName>TestLoggerFichierConfig</ApplicationName>
<LogName>AgentLogger</LogName>
</appender>
I read that the event log must be restarted when adding a new log so the recent events will be displayed in it. I did this, and it allowed me to see the new log that has been created, but nothing will go there. Is it possible to force Log4Net to stop using the previous log to rather use the one I defined in the app.config?
Thanks for the help.
Update:
It seems that le EventLogs recognizes the source application and decide in wich log to put the new events since he remembers where it previously went. When I first set the logName, it works fine. If I stop the app, change the logname and restart it, the event will still go to the previous log. If the logName does not exists in the event log, it will be created, but not used! There might be something to do, but it is not in the side of log4net, and it may be dangerous to change the windows settings at this level. I created two eventLogAppender on the same app.config file, both pointing at different logs. the events got to the same log though. It is being obvious that the problem doesn't come from log4net, and the solution to my problem will not be solved by code. Thanks for the great advices though.
Something is probably wrong with your configuration and it is simply not telling you about it because log4net itself is designed to never, ever throw errors.
You can turn on internal debugging for log4net, but don't deploy to production with log4net debugging turned on. Weirdness will eventually happen if deploy your product with the log4net debugging switch on.
http://haacked.com/archive/2006/09/27/Log4Net_Troubleshooting.aspx
http://logging.apache.org/log4net/release/faq.html (see the troubleshooting section)
I ran into this and it wasn't down to log4net but the re-mapping of the source to a new custom log. I took log4net out of the equation and finding that the problem occurred usint the EventLog class directly, I eventually found EventLog.CreateEventSource is not creating a custom log
I think it's also the reason behind this Windows service always writes to custom log and application log
First and foremost, I'd encourage you to turn on internal debugging, as found in the link ntcolonel posted. Add this to your config file:
<add key="log4net.Internal.Debug" value="true"/>
Perhaps I am not understanding your question entirely, but I'd like to ensure you have both created the appender and properly tell log4net that you want to actually use that appender:
<log4net debug="true">
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="Logs\SomeApplication.xml"/>
<appendToFile value="true"/>
<rollingStyle value="Size"/>
<countDirection value="1"/>
<maxSizeRollBackups value="30"/>
<maximumFileSize value="10MB"/>
<staticLogFileName value="true"/>
<lockingModel type="log4net.Appender.FileAppender+MinimalLock"/>
<layout type="log4net.Layout.XmlLayoutSchemaLog4j">
<locationInfo value="true"/>
</layout>
</appender>
<appender name="SmtpAppender" type="log4net.Appender.SmtpAppender">
<to value="SomeDistributionList#somehost.com"/>
<from value="SomeApplication#somehost.com"/>
<subject type="log4net.Util.PatternString" value="Some Application Error - %property{log4net:HostName}"/>
<smtpHost value="smtp.somehost.com"/>
<bufferSize value="1"/>
<threshold value="ERROR"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %property{log4net:HostName} %logger %level %newline%newline%property{ExceptionThrottleInformation}%newline%newline%message%newline"/>
</layout>
<filter type="SomeNamespace.SomeSubNamespace.Log4Net.ExceptionThrottleFilter, SomeSubNamespace">
<threshold value="10"/>
<thresholdTimeoutSeconds value="60"/>
<timeoutSecondsBetweenMessages value="600000"/>
<exceptionText value="Timeout expired"/>
</filter>
</appender>
<appender name="DatabaseAppender" type="SomeNamespace.SomeSubNamespace.DatabaseTraceAppender, SomeSubNamespace">
<hoursToKeepInformationalTraces value="48"/>
<hoursToKeepErrorTraces value="96"/>
<threshold value="INFO"/>
</appender>
<root>
<level value="INFO"/>
<appender-ref ref="RollingFileAppender"/>
<appender-ref ref="SmtpAppender"/>
<appender-ref ref="DatabaseAppender"/>
</root>
Note that while I have multiple appenders, I reference which to call within the root tag. Since you didn't post your whole config file, it's tough to tell if everything is matching as it should.
Under my VS2010 solution I've this situation:
WEBSITE
Library1
Library2
On global.asax.cs I initialize the log4net configuration using:
private static log4net.ILog _logger = log4net.LogManager.GetLogger("globalASAX");
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
log4net.Config.XmlConfigurator.Configure();
_logger.Info("[APPLICATION START] " + DateTime.Now.ToString());
}
It works fine and Application start message is correclty available on log.txt file. The problem happens when I try to use log something on the classes available on DLL Library1 or Library2.
I added the row:
private static log4net.ILog _logger = log4net.LogManager.GetLogger(typeof(ImageRepository));
but when I try to all the _logger.error("blabla") nothing happens on log file; all properties of _logger are false (i.e. isdebugenable=false). How can I fix that? I followed the instruction available here:
http://logging.apache.org/log4net/release/manual/configuration.html
the configuration of log4net is under web.config file:
<log4net>
<appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
<file type="log4net.Util.PatternString">
<conversionPattern value="log\explorer-log-%date{ yyyy.MM.dd.HH.mm.ss}-[%processid].log"/>
</file>
<appendToFile value="true"/>
<maximumFileSize value="1024KB"/>
<maxSizeRollBackups value="5"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %level %logger - %message%newline"/>
</layout>
</appender>
<root>
<level value="DEBUG"/>
<appender-ref ref="RollingFile"/>
</root>
</log4net>
anyone can help me?
thanks,
Andrea
I suspect log4net cannot find the logger for your type ImageRepository. As a quick check create a named logger and try calling it.
private static log4net.ILog _logger = log4net.LogManager.GetLogger("FooLog");
And config
<root>
<level value="DEBUG"/>
<appender-ref ref="RollingFile"/>
</root>
<logger name="FooLog">
<level value="DEBUG"/>
<appender-ref ref="RollingFile"/>
</logger>
In theory this should work (as I'm sure you are aware). In general, however, there are two areas to check when you don't get anything logged to a file. The first area I would check would be the file appender itself. Text files can be locked, which can cause messages to be lost. The other thing I would check is to be sure that log4net is properly initialized in the library. It should be, but it doesn't hurt to check. If neither of these solutions help, try turning on the debugging of log4net itself to see what error messages are coming up. Here is a link that shows you how to turn on those messages:
http://haacked.com/archive/2006/09/27/Log4Net_Troubleshooting.aspx