In MS Enterprise Library 5.0, Logging application block, at runtime, Can I get the name of the log file (Flat File Listener) to which the log is going to?
Asked
Active
Viewed 1,553 times
1
-
Did you tried _anything_? – Soner Gönül Apr 30 '13 at 08:43
-
I tried the various methods available on the LogWriter, and tried finding on the internet if somebody had the same question.. – ViV Apr 30 '13 at 08:58
2 Answers
2
You can get that information by using the configuration objects:
IConfigurationSource configSource = ConfigurationSourceFactory.Create();
var logSettings = configSource.GetSection(LoggingSettings.SectionName) as LoggingSettings;
var flatFileTraceListener = logSettings.TraceListeners
.First(t => t is FlatFileTraceListenerData) as FlatFileTraceListenerData;
string fileName = flatFileTraceListener.FileName;
This assumes that you are interested in the first trace listener that is a FlatFileTraceListener. If you wanted to get the trace listener by type and name then you could do that too:
IConfigurationSource configSource = ConfigurationSourceFactory.Create();
var logSettings = configSource.GetSection(LoggingSettings.SectionName) as LoggingSettings;
var flatFileTraceListener = logSettings.TraceListeners
.FirstOrDefault(t => t is FlatFileTraceListenerData && t.Name == "Flat File Trace Listener")
as FlatFileTraceListenerData;
string fileName = flatFileTraceListener.FileName;

Randy Levy
- 22,566
- 4
- 68
- 94
0
Change the Config is no problem.
After Change the filename property aaa.log -> bbb.log the Logger not Write in the new Filename.
The changed config must saved/activated (?) or the Logger must new Initialized..!??
IConfigurationSource configSource = ConfigurationSourceFactory.Create();
var logSettings = configSource.GetSection(LoggingSettings.SectionName) as LoggingSettings;
var rollFileTraceListener = logSettings.TraceListeners
.FirstOrDefault(t => t is RollingFlatFileTraceListenerData && t.Name == "RollingFlatFileTraceListener")
as RollingFlatFileTraceListenerData;
string fileName = rollFileTraceListener.FileName;
rollFileTraceListener.FileName = fileName.Replace("aaa", "bbb");
LogWriterFactory f = new LogWriterFactory(configSource);
f.Create();
Logger.Reset();
LogEntry logEntry = new LogEntry();
logEntry.Message = $"{DateTime.Now} Count:{333}";
logEntry.Categories.Clear();
logEntry.Categories.Add("General");
Logger.Write(logEntry);