Reputation: 3338
I have a windows-service project in C#.NET with recent version of .NET framework and i am trying to implement nLog 4.5.11 into the project. Windows service has a timer class and its events gets fired every 10 secs. I am trying to write to nLog from these handlers but it is not printing in the log file.
The info statements from the windows onstart event handler is getting written to the log fine perfectly fine.
After troubleshooting in the internalLogFile.log, now i found those lines of nLog Info, but in another log file created by some other assembly such as RabbitMQ.log. I did not even mention it in the nLog.config. I am not sure why nLog is doing this mixing of content in multiple files.
nlog assemblies added to the windows-service project:-
nLog.config file content :-
<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" internalLogFile="C:\Px\log.txt" internalLogLevel="Info">
<targets>
<target name="logconsole" xsi:type="Console" />
<target name="logfile"
xsi:type="File"
fileName="${basedir}/Px_LoaderService-${shortdate}.log" />
</targets>
<rules>
<logger name="docLogger" minlevel="Info" writeTo="logfile" />
<logger name="*" minlevel="Info" writeTo="logconsole" />
</rules>
</nlog>
InternalLogfile also does not have any error or information about the missing writes.
Here is sample code of the Timer event handler :-
private static NLog.Logger log = LogManager.GetLogger("docLogger");
private void BulkInserts(object sender, ElapsedEventArgs e)
{
lock (_locker)
{
try
{
log.Info(string.Format("BulkInsert called."));
}
}
Please suggest where am i making the mistake & this mix of log writing is happening.
Upvotes: 0
Views: 264
Reputation: 3338
i found the problem in my windows-service project. Another nlog file was getting generated from a different assembly reference within the project. That is why some log statements were written to different log file. Since log is a static object and created at the class level and second log object was getting created within the assembly with a different class scope. But the log object is of type static and that is what caused the issue. I would say its static object contamination from two different classes.
Now i have identified the issue and it is resolved
Upvotes: 1