0

Right now whenever I run application It creates a file (file name specified in Web.Config Logging Section).

What I want is to change the path and name of the file (based on GUID passing as query sting) at run time.

I did some research, but examples available are not working for me. Will appreciate if someone can provide a code sample.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Emahum
  • 15
  • 7
  • Can you expand a bit on the requirements? Are the log files based on a user (each user gets their own log file/folder)? – Randy Levy Mar 27 '12 at 17:39
  • I am loading different DLLs when passing an ID in query string. So If I pass ID 12345, my web application will load 12345.dll If I pass ID 67890 in query string. my web application will load 67890.dll and do whatever it does. Right now in EL I do have Flat File Listener which creates log file (LogInfo.log)and when application is finished running I am just renaming the file to whatever is my ID. The problem is when I am running the same application twice at the same time with 2 different IDs. – Emahum Mar 28 '12 at 02:07
  • I just wanted to create folder and file name based on my ID I am passing and log the info in respective folder and file. – Emahum Mar 28 '12 at 02:07
  • Please don't prefix yout titles with "C# Enterprise Library 5.0 - " and such. That's what the tags are for. – John Saunders Mar 28 '12 at 03:43

2 Answers2

0

If you just need to change the trace log file at runtime, you can simply remove the current tracelistener using

System.Diagnostics.Trace.Listener.Remove("Listener1");

Then add a new Listener with the new log file name as,

System.Diagnostics.Trace.Listener.Add(new System.Diagnostics.TextWriterTraceListener("NewLogFileName.txt","Listener2");

Now your logs will be recorded to NewLogFileName.txt

Vignesh Iyer
  • 300
  • 3
  • 6
0

As you've discovered, your requirements are not a natural fit for the configuration based approach of Enterprise Library. That's not to say that it can't be done.

One way to accomplish what you want is to use programmatic configuration of the logging block.

One approach would be to create an IDictionary to hold a mapping of IDs to LogWriters. When an ID comes in check to see if a LogWriter already exists in the IDictionary. If it does then use it and if it doesn't then create a new LogWriter.

Here is some sample code that uses the Logging Fluent Interface to configure a UnityContainer and then resolves a LogWriter and saves it to a Dictionary:

int id = 123123;
Dictionary<int, LogWriter> loggers = new Dictionary<int, LogWriter>();

ConfigurationSourceBuilder builder = new ConfigurationSourceBuilder();

builder.ConfigureLogging()
        .WithOptions
            .DoNotRevertImpersonation()
        .SpecialSources.LoggingErrorsAndWarningsCategory.SendTo.FlatFile("Flat File Listener").ToFile(@"trace.log")
        .LogToCategoryNamed("General")
            .WithOptions.SetAsDefaultCategory()
            .SendTo.FlatFile("AppSpecificFlatFile" + id)
            .ToFile("logging" + id + ".log")       
            ;

DictionaryConfigurationSource configSource = new DictionaryConfigurationSource();
builder.UpdateConfigurationWithReplace(configSource);
coreExtension = new EnterpriseLibraryCoreExtension(configSource);

IUnityContainer container = new UnityContainer();
container.AddExtension(coreExtension);

var logger = container.Resolve<LogWriter>();

loggers[id] = logger;

This is just a sample and is not thread safe or abstracted but should hopefully help.

Randy Levy
  • 22,566
  • 4
  • 68
  • 94