NLog skips log messages..NET problem? Multithreaded problem?

I am running a multithreaded C # .NET 4.5 application.

Running on my local Windows 7 laptop, NLog logs all my messages. I publish the application using Visual Studio ... copy the published application to Windows Server 2008 ... and run the application on the server: this leads to a missed log message. Can someone help me understand WHY and how can I fix this or suggest an alternative to NLog?

My configuration file is as follows:

I tried this without "async = true"

<targets async="true"> <target xsi:type="ColoredConsole" name="ColoredConsole" layout="${date} ${level} ${message} ${event-context:item=AlgID} " /> <target name="xmlfile" xsi:type="File" fileName="C:\QRT\Logs\LogEmiter.Nlog.xlog" keepFileOpen="true" layout="${log4jxmlevent}" /> <target xsi:type="File" name ="LogFile" fileName="C:\QRT\Logs\QRTLog-${shortdate}.log" layout ="${date:format=yyyy-MM-dd HH\:mm\:ss.fff}|${level}|${threadid}|${threadname}|${event-properties:item=FromID}|${message}${exception:format=tostring} "/> <target xsi:type="File" name ="TapeLogFile" fileName="C:\QRT\Logs\QRTMarketLog.txt" layout ="${date:format=yyyy-MM-dd HH\:mm\:ss.fff}|${level}|${message}"/> </targets> <rules> <!-- add your logging rules here --> <logger name ="TapeLogFile" minlevel="Trace" writeTo="TapeLogFile" /> <logger name ="TapeLogFile" minlevel="Trace" writeTo="ColoredConsole" final="true"/> <logger name="*" minlevel="Trace" writeTo="ColoredConsole" /> <logger name="*" minlevel="Trace" writeTo="xmlfile" /> <logger name="*" minlevel="Trace" writeTo="LogFile" /> <!-- Write all events with minimal level of Debug (So Debug, Info, Warn, Error and Fatal, but not Trace) to "f" <logger name="*" minlevel="Debug" writeTo="f" /> --> </rules> </nlog> 

I log messages as follows:

In my typical class, I would initialize my helper class NLog as such:

  private void InitliazlieLogger() { LogManager.ThrowExceptions = true; m_logger = new NLogHelper(LogManager.GetCurrentClassLogger()); m_logger.Set("FromID", "Email"); // Class setting. } private void DoSomething(int _x) { m_logger.Debug ("Print this statement: {0}", _x); } 

I have an NLog helper class ...

 public NLogHelper(Logger Logger) { m_logger = Logger; m_properties = new Dictionary<string, object>(); } public void Debug(string Message, params object[] Args) { m_logger.Debug() .Message(Message, Args) .Properties(m_properties) .Write(); notify(Severity.Debug, Message); } 

The problem is that some log messages are simply skipped. I added my own logging class, which wrote the file manually, and inserted the call into NLogHelper.Debug, and I found that the file I wrote manually had a full set of log messages, but there was no NLog output.

Again, this works on my laptop, but it doesn’t work on Windows Server 2008. My server has .NET 4.6.1 installed. It seems that I only have 4.5.2 on my Windows 7 machine. Could this be?

As a note: if I upload all my source files to the server and use Visual Studio on the server to compile the same code, does NLog seem to work?!?

-Confused.

Thanks red

+5
source share
2 answers

EDIT: This is not a complete answer. The answer below solved half the problem. I did not notice that the missing messages were INFO, not DEBUG. Converting all my INFO logs to DEBUG logs fixed my problems. It appears that although I can output INFO logs to my local computer, INFO will work on my server. Still not caused by roots. :(

Well, I thought that the only difference between the working system (my laptop running Windows 7) and the server (running Window Server 2008) is the version of Windows and the installed version of .NET.

The problem was that for Cybers, this was the reason that there were several hardware processors on the server, and multithreading was closer to real multithreading than on my laptop. NLog returned error messages, but I did not see them. He tried to write over himself.

So, I (Siberians) have changed:

 <target xsi:type="File" name ="LogFile" fileName="C:\QRT\Logs\QRTLog-${shortdate}.log" layout ="${date:format=yyyy-MM-dd HH\:mm\:ss.fff}|${level}|${threadid}|${threadname}|${event-properties:item=FromID}|${message}${exception:format=tostring} "/> 

to

  <target name="asyncWrapper" xsi:Type="AsyncWrapper" queueLimit="20000"> <target name="LogFile" xsi:type="File" fileName="C:\QRT\Logs\QRTLog-${shortdate}.log" layout="${date:format=yyyy-MM-dd HH\:mm\:ss.fff}|${level}|${threadid}|${threadname}|${event-properties:item=FromID}|${message}${exception:format=tostring} " concurrentWrites="true" /> </target> 

The problem is fixed. Man ... it took me a month! I was about to give up and write my own registrar manually.

Red

+1
source

I use the following code and work fine.

  public class ServerDataSource : IDataSource { private Logger _log; public ServerDataSource() { _log = LogManager.GetLogger("ServerDataSource"); } public bool DoSomething() { try { _log.Info("Doing something"); } catch (Exception ex) { _log.Error("Error occurred" + ex.Message); } } } 

The configuration was as follows:

  <configSections> <section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog"/> </configSections> 

0
source

Source: https://habr.com/ru/post/1240480/


All Articles