Filename as parameter

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Filename as parameter

andrecarlucci
Hello,

Is it possible to do something like this?

logger.Debug("my message","filename1.log");
logger.Debug("my message","filename2.log");
logger.Debug("my message", myVar);

My problem is that I need to log in different files, but the file names will be generated on the fly. So there is no way to know them in advance.

Thanks,

Andre
Reply | Threaded
Open this post in threaded view
|

Re: Filename as parameter

Jarek Kowalski
Administrator
andrecarlucci wrote
Hello,

Is it possible to do something like this?

logger.Debug("my message","filename1.log");
logger.Debug("my message","filename2.log");
logger.Debug("my message", myVar);

My problem is that I need to log in different files, but the file names will be generated on the fly. So there is no way to know them in advance.

Thanks,

Andre
The simple thing that comes to mind is to use separate loggers for every message and define logging rule where file name is based on ${logger}. Alternatively you can use ${event-context}.
Unfortunately this is not very well documented, see here for some example:

http://www.nabble.com/event-context-needs-more-documentation-td17740567s6167.html

Jarek
NLog Blog
Reply | Threaded
Open this post in threaded view
|

Re: Filename as parameter

andrecarlucci
Hi Jarek,

Thanks a lot, that solved my problem (${event-context}).

André
Reply | Threaded
Open this post in threaded view
|

Re: Filename as parameter

jkfriends
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
Reply | Threaded
Open this post in threaded view
|

Re: Filename as parameter

jkfriends
I found a simple solution by looking at the similar thread.

NLog.MDC.Set("UserId", "Oper11"); // At the top of every API where I want to log information.

This solves my requirement.  I hope that this is thread-safe.  Could someone from NLOG team confirm this?

Thanks again, JK