Using NLog with multiple application instances

I have a WinForms application deployed by ClickOnce. Users can run multiple instances of the application. Each instance must be written to a separate log file. I need to find the current instance log file at runtime, so I can use it to send crash reports, etc.

Now I can not get around the requirement of multiple instances. (They really need a tabbed interface, but this is one big mess of the old application, and I just can't implement it right now.)

In general logging mode, but individual application instances overwrite the same file.

I tried adding a timestamp to the log file name, but this did not work:

<target name="logfile" xsi:type="File" 
              fileName="${specialfolder:folder=ApplicationData}/MyCompany/CRM/crm_${longdate}.log"
              deleteOldFileOnStartup ="true"
              layout="${longdate} ${message} ${exception:format=tostring}"/>

Is this even possible?

+4
source share
1 answer

In the context of your application, can you find out the real (and meaningful) name for this instance? Can I run multiple instances with a command line parameter that represents the name of the application? If you can “know” a useful name for the application, you can save this name in the GlobalDiagnosticsContext and use the GlobalDiagnosticsContext LayoutRenderer to create your file name.

So your configuration might look something like this:

<target name="logfile" xsi:type="File" 
          fileName="${specialfolder:folder=ApplicationData}/MyCompany/CRM/crm_${gdc:item=application}.log"
          deleteOldFileOnStartup ="true"
          layout="${longdate} ${message} ${exception:format=tostring}"/>

In the application startup logic, you should install gdc as follows:

GlobalDiagnosticsContext.set("application", GetApplicationNameFromCommandLineArgs());

Each application will write to its own log file, named based on the command line parameter.

Good luck

+3
source

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


All Articles