There is some code:
private const string FORMAT_TEXT =
"{timestamp}: [{category} ({win32ThreadId}-{threadName}) {severity}] |{title}|: {message}";
string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "trace.log");
var builder = new ConfigurationSourceBuilder();
builder.ConfigureLogging()
.WithOptions.DoNotRevertImpersonation()
.LogToCategoryNamed("Category1")
.SendTo.FlatFile("MyMessages1")
.FormatWith(new FormatterBuilder()
.TextFormatterNamed("Text Formatter 1").UsingTemplate(FORMAT_TEXT))
.ToFile(path)
.LogToCategoryNamed("Category2")
.SendTo.FlatFile("MyMessages2")
.FormatWith(new FormatterBuilder()
.TextFormatterNamed("Text Formatter 2").UsingTemplate(FORMAT_TEXT))
.ToFile(path);
var configSource = new DictionaryConfigurationSource();
builder.UpdateConfigurationWithReplace(configSource);
EnterpriseLibraryContainer.Current = EnterpriseLibraryContainer.CreateDefaultContainer(configSource);
for (int i = 0; i < 10; i++)
{
Logger.Write("Error message: " + i.ToString(), "Category1");
Logger.Write("Error message: " + i.ToString(), "Category2");
}
Console.ReadKey();
It produces two log files: trace.log for Category1 and 65a25bb0-4c42-430d-b2b7-9bd2c8ea41e1trace.log for Category2. I want logging to one file trace.log. What is wrong?
You want to use a SharedListener so that both categories log to the same listener. In your code you are creating two separate listeners that both log to the same file (which is causing file locking issues).
Try: