EagleBeak
EagleBeak

Reputation: 7419

Using NLog with multiple app instances

I have a ClickOnce-deployed WinForms app. Users can launch multiple instances of the app. Each instance should write to a separate log file. I need to be able to discover the current instance's log file at runtime, so I can use it for sending out crash reports, etc.

Now I can't get around the multiple instances requirement. (What they really need is a tabbed UI, but this is one big mess of a legacy app, and I just can't implement it right now.)

In general logging works, but separate app instances overwrite the same file.

I have tried to add a time stamp to the log file name, but that didn't 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 possible at all?

Upvotes: 5

Views: 1241

Answers (1)

wageoghe
wageoghe

Reputation: 27608

Within the context of your application, can you know a valid (and meaningful) name for that instance? Can the multiple instances be launched with a command line parameter that represents the name of the application? If you can "know" a useful name for the application, then you could store the name in the GlobalDiagnosticsContext and use the GlobalDiagnosticsContext LayoutRenderer to build your filename.

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 your application startup logic, you would set the gdc like this:

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

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

Good luck!

Upvotes: 3

Related Questions