log4net info hangs console application if called from a worker thread - c#

I have a problem where log4net hangs on a info statement. I have a C# multithreaded console application. The main thread creates several working threads that log using log4net, writing to a file and to the console. The logger is a static variable in some base class. At some random point into executing the application the logger will hang on a info statement, but only on some machines. I came across some post about rare cases where on specific system setups the log write buffer will fill and log4net will wait for it to empty, what never happens. Thus the application hangs. In my case the logger writes into the file, but then stops and the message does not reach the console. Pressing ctrl-c on the console actually causes the application to continue normally.
I tried to make the logger a member of my class instead of having it static as suggested here: http://apache-logging.6191.n7.nabble.com/Multi-threaded-ASP-NET-application-not-working-td21782.html
But that just caused my application to hang at the first log statement there was.
This is the call where it hangs:
_logger.Info($"Generation: {_ea.CurrentGeneration}/{_maxGenerations}, " +
$"Time/gen: {timePerGen} ms, Est. time remaining: {Utilities.TimeToString(timeRemainingEst)} " +
$"Fitness - Max: {_ea.Statistics._maxFitness:F4} Mean: {_ea.Statistics._meanFitness:F4}, " +
$"Complexity - Max: {_ea.Statistics._maxComplexity:F0} Mean: {_ea.Statistics._meanComplexity:F2} " +
$"Champ: {_ea.CurrentChampGenome.Complexity:F0} Strategy: {_ea.ComplexityRegulationMode}, " +
$"Specie size - Max: {_ea.Statistics._maxSpecieSize:D} Min: {_ea.Statistics._minSpecieSize:D}, " +
$"Generations since last improvement: {_ea.CurrentGeneration - _lastMaxFitnessImprovementGen}"
);
Has anyone encountered similar issues and know how to fix it or a workaround?
Here is the requested log4net config
<log4net>
<appender name="FileAppender" type="log4net.Appender.FileAppender">
<file type="log4net.Util.PatternString" value="ENTM/%property{name}/%property{id}/%property{count}/log.txt" />
<appendToFile value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%utcdate{yyyy-MM-dd HH:mm:ss} [%thread] %-5level: %message%newline" />
</layout>
</appender>
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
<target value="Console.Error" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%utcdate{yyyy-MM-dd HH:mm:ss} [%thread] %-5level: %message%newline" />
</layout>
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="FileAppender" />
<appender-ref ref="ConsoleAppender" />
</root>
</log4net>

Related

Log4net not working in multithreading in C#

hello friends i have added log4net package form nuget packages. for sync call log4net add logs in log file but when i am declaring a methods using Task its not working
<log4net>
<appender name="TestAppender" type="log4net.Appender.RollingFileAppender">
<file value="D:\log\logswebapi.log" />
<encoding value="utf-8" />
<appendToFile value="true" />
<rollingStyle value="Date" />
<layout type="log4net.Layout.PatternLayout">
<!--<conversionPattern value="%date %level [%thread] %type.%method - %message%n" />-->
</layout>
</appender>
<root>
<level value="All" />
<!-- If the following line is not included the log file
will not be created even if log4net is configured with this file. -->
<appender-ref ref="TestAppender" />
</root>
</log4net>
i have added this code in web.config file
private void AddLog(string str)
{
ILog Log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
Log.Info(DateTime.Now + " - " + str);
}
public string SendDocument(){
// send document logic
AddLog(send document done");
}
public string Response(){
Task.Run(() =>SendDocument()); // here i am calling this senddocument method in thread
}
in above code Response method i am calling send document methods in thread but logs are not added now how to resolve this issue ?
thanks in advance

Log4Net RollingFileAppender losing messages

I am using log4net to log to files and for some reason, some log messages are lost, and always in the same spot. First my configuration:
<log4net>
<root>
<level value="ALL" />
<appender-ref ref="RollingFileAppender" />
</root>
<appender name="aiAppender" type="Microsoft.ApplicationInsights.Log4NetAppender.ApplicationInsightsAppender, Microsoft.ApplicationInsights.Log4NetAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%message%newline" />
</layout>
</appender>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="logs\import.txt" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="10MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>
</log4net>
The aiAppender, while defined, is not used.
The log pattern is that I initially burst log about 88 lines of which all are there. Then the missing log period starts and lasts approx 6 seconds. Then the logging continues and everything is logged from that point onward.
If I remove the burst logging and replace it with only a few log entries, nothing is lost.
Apparently RollingFileAppender can lose messages. I wasn't aware of that.
I am certain this is an issue with log4net, as I have a log-wrapper where I split my log messages to both log4net and to application insights (the aiAppender didn't do a good enough job for us) and application insights does receive the messages that log4net doesn't log.
Can I configure the RollingFileAppender to avoid losing messages?
Edit: The log wrapper logs first to log4net and then to application insights so it is not a question about the application insights logging that prevents log4net logging.
You can only debug this when you activate internal debugging. Active the debugger and see if for example log4net is reconfigured at some point.

Log4net sometimes creating two logs, one empty

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);
...
}

log4net BasicConfigurator is not printing on console

I decided to use log4net to handle all the logging activities for the application I am working on... after reading through documentation, I decided to start making experiments, but failed miserably at the very first attempt
The application is being developed with c# using MonoDevelop 3.0.3.2 in a fairly standard Debian 7 distribution, in order to later be deployed on Linux machines.
The code is trivial so far and follows the very early steps in the log4net documentation, and is quoted at the end of the post.
The problem is that the logger does not print anything to console; while trying to understand what's going wrong, I looked intoo it with the debugger; I see that the log reference contains the AppendersCollection, which unfortunately is empty I guess that something is not working properly during configuration and I have no appenders, hence there is no log output.
What am I missing? any suggestion? Is there some sort of a known issue for the combination of MonoDevelop-Linux-log4net I am not aware of?
using System;
using log4net;
using log4net.Config;
namespace TestLog4Net
{
class MainClass
{
private static readonly ILog log = LogManager.GetLogger(typeof(MainClass));
public static void Main (string[] args)
{
BasicConfigurator.Configure();
Console.WriteLine ("Hello World!");
log.Info("Hello World!");
}
}
}
UPDATE
I did some tests loading the configuration file.
I created 3 loggers each with its own appender, two are rolling file appenders, and one is the console appender.
Then I log "Hello World!" on each logger.
The rolling file loggers work as expected, while the output from the ConsoleLogger is nowhere to be found. I checked with the Debugger and the clogger object does NOT display an empty AppenderCollection, but there no output anyway. Below you can find the updated code along with the configuration file.
using System;
using log4net;
using log4net.Config;
namespace TestLog4Net
{
class MainClass
{
public static void Main (string[] args)
{
XmlConfigurator.Configure(new System.IO.FileInfo("app.config"));
ILog rflogger1 = LogManager.GetLogger("RFLoggerOne");
ILog rflogger2 = LogManager.GetLogger("RFLoggerTwo");
ILog clogger = LogManager.GetLogger("CLogger");
rflogger1.Info("Hello World!");
rflogger2.Info("Hello World!");
clogger.Info("Hello World!");
Console.WriteLine ("Hello World!");
}
}
}
<log4net>
<appender name="CAppender" type="log4net.Appender.AnsiColorTerminalAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>
<appender name="RFAppender1" type="log4net.Appender.RollingFileAppender">
<file value="rf1.log"/>
<appendToFile value="true"/>
<rollingStyle value="Composite"/>
<datePattern value="yyyyMMdd"/>
<maxSizeRollBackups value="10"/>
<maximumFileSize value="1000000"/>
<staticLogFileName value="true"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %level %logger - %message%newline"/>
</layout>
</appender>
<appender name="RFAppender2" type="log4net.Appender.RollingFileAppender">
<file value="rf2.log" />
<appendToFile value="true" />
<maximumFileSize value="100KB" />
<maxSizeRollBackups value="2" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%level %thread %logger - %message%newline" />
</layout>
</appender>
<logger name="CLogger">
<level value="ALL"/>
<appender-ref ref="CAppender"/>
</logger>
<logger name="RFLoggerOne">
<level value="ALL"/>
<appender-ref ref="RFAppender1"/>
</logger>
<logger name="RFLoggerTwo">
<level value="ALL"/>
<appender-ref ref="RFAppender2"/>
</logger>
<root>
<level value="ALL"/>
</root>
</log4net>
Which kind of project do you use? A Console Application?
There are several cases you wont see any output on std::out or std::err on your console.
Have a look at this post:
Can one executable be both a console and GUI application?

log4net GenericFailure. Unable to acquire lock on file

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

Categories