I just created a new simple console app in VS2022 for .NET 6.0 - all default settings.
Then I added to project:
In app.config I put following code (just from standard example from microsoft):
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.diagnostics>
<trace autoflush="false" indentsize="4">
<listeners>
<clear/>
<add name="myListener" type="System.Diagnostics.TextWriterTraceListener" initializeData="TextWriterOutput.log" />
<remove name="Default" />
</listeners>
</trace>
</system.diagnostics>
</configuration>
In program.cs I have following code:
// See https://aka.ms/new-console-template for more information
using System.Diagnostics;
Console.WriteLine("Hello, World!");
var x = new System.Diagnostics.TextWriterTraceListener();
Trace.WriteLine("abs-test");
Console.WriteLine(string.Format("--- {0} trace listeners", Trace.Listeners.Count));
int idx = 0;
foreach (TraceListener lsnr in Trace.Listeners)
{
idx++;
Console.WriteLine(string.Format(" #{0}: {1}", idx, lsnr));
}
Console.WriteLine("ok...");
Then I run app and... TRACE LOG WAS NOT CREATED AT ALL!
So, no errors, it started and finished, but no trace output. Also - in list of trace listeners I see only 1 element - "System.Diagnostics.DefaultTraceListener".
It looks like .NET 6.0 app completely ignores any configuration in <system.diagnostics> section in app.config! But why?!
How to make it working like it worked in .NET 2.x .. 4.x frameworks?!
As already outlined in the comment above, .NET 6.0 did not consider the app.config file when initializing the Trace system.
So unless you manually read the app.config file (with or without using System.Configuration.ConfigurationManager.GetSection(), etc.) and configure the Trace from that, there is no way this will happen in .NET 6.0.
The reason given in comments on GitHub is that, the System.Configuration.ConfigurationManager package did not exist in .NET Core at the time the System.Diagnostics.Trace* stuff was initially ported for .NET Core.
The functionality is available, starting with .NET 7.0. However, you need to manually ensure that initialization happens based on the app.config file. This is necessary, because nothing .NET (Core) causes the ConfigurationManager to be initialized automatically (by the runtime).
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.diagnostics>
<trace autoflush="false" indentsize="4">
<listeners>
<clear/>
<add name="myListener" type="System.Diagnostics.TextWriterTraceListener" initializeData="TextWriterOutput.log" />
<remove name="Default" />
</listeners>
</trace>
</system.diagnostics>
</configuration>
The app.config is basically unchanged from your version.
Make sure that you add a reference to System.Configuration.ConfigurationManager.nupkg at least in version 7.0.0 (the 6.x versions do not include the TraceConfiguration
class).
// See https://aka.ms/new-console-template for more information
using System.Diagnostics;
Console.WriteLine("Hello, World!");
// Initialize Trace configuration from app.config
TraceConfiguration.Register();
// ...
Note that you will not see any trace messages that are issued from code that runs before the TraceConfiguration.Register()
line is executed. This could be the case if Trace is used from code that runs from static initialization.