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.
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.
If you just need to change the trace log file at runtime, you can simply remove the current tracelistener using
Then add a new Listener with the new log file name as,
Now your logs will be recorded to NewLogFileName.txt