简体   繁体   中英

NLog application level variables

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM