Hi, I'm new to NLog logging system. I have been trying to integrate this into my WCF application (in the service layer).
My requirement is to make a new log file for every user that logs in in the client side. When an API is invoked from the client via proxy to the service API, I would like to store the log information for that user in a separate log file.
As you can imagine that the service layer is state-less which means that it doesn't maintain the information as who is logging in. It could be 500 users going through the same service layer at any given time. This means that the service layer does not keep track of who's on-line.
To accomplish the single log file per user, I think event-context attribute would help me to achieve that but I don't know how to use it.
In the ctor of the service implementation, I do this for all users:
LoggingConfiguration config = new LoggingConfiguration();
// Step 2. Create targets and add them to the configuration
FileTarget fileTarget = new FileTarget();
config.AddTarget("file", fileTarget);
// Step 3. Set target properties
fileTarget.FileName = "c:\\temp\\${event-context:item=UserId}-${shortdate}-jkjk.txt"; fileTarget.Layout = "${message}";
// Step 4. Define rules
LoggingRule rule2 = new LoggingRule("*", LogLevel.Debug, fileTarget);
config.LoggingRules.Add(rule2);
// Step 5. Activate the configuration
LogManager.Configuration = config;
logger = LogManager.GetLogger("XYZ");
Then in the individual API, I do this at the top of the function.
evt = new NLog.LogEventInfo(NLog.LogLevel.Debug, "API-1", "My Debug Msg");
evt.Context["UserId"] = userId; // the userId variable holds the value of the user id.
logger.Log(evt);
The problem is that in order to log a message in the correct file, I had to recreate the LogEventInfo structure every time that I want to do a log write.
After the above 3 lines, if I do this, it does not go into the same file but goes into another file (-2008-09-28-jkjk.txt) file.
logger.Debug("Another Message");
What is the problem here? How do I efficiently use NLog in WCF context the way I'm using?
Also I noticed that the NLOG does not hold a lock on the log file which means that someone can delete the file while it's still writing.
Any help would be appreciated.
Thanks, JK