Reputation: 2580
I'm tasked with a WPF app & have chosen NLog for our logging.
I note answers here and here I would like to do something similar but with .net Desktop & with more control.
I would like to set a guid once at app start and each log entry (at any level) have this guid included automatically, so I can search on that guid. Creating a new guid when the user logs in.
Going further it would actually be good knowledge to know if I can programmatically add any variables that get included with every Log I make, so I dont have to add them to each and every Log.AnyLevel, an example output:
DateTime | LoginID | SessionGuid | MyMessage
to get this output I like to only do:
logger.Trace("MyMessage")
LoginID & SessionGuid being set either when App starts or I instantiate a ViewModel or the User Logs in.
I note at present I can do this per log with
LogEventInfo
How can I add a variable once (from code) and have it always included?
Upvotes: 1
Views: 569
Reputation: 19867
You can setup a NLog GDC variable:
GlobalDiagnosticsContext.Set("AppGuid",Guid.NewGuid().ToString());
And then reference that in your layouts:
${gdc:item=AppGuid}
See also https://github.com/NLog/NLog/wiki/Gdc-layout-renderer
Upvotes: 2