5

TraceSource is accessible from System.Diagnostics in my ASP .NET Core project.

In src file you can find header:

#region Assembly System.Diagnostics.TraceSource, Version=4.1.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
// C:\Program Files\dotnet\sdk\NuGetFallbackFolder\microsoft.netcore.app\2.2.0\ref\netcoreapp2.2\System.Diagnostics.TraceSource.dll
#endregion

What does it mean? Is Version of .Net Famework >=4.1.1.0 acceptable? Is TraceSource included in some version of .Net Standard?

UPD MY RESOLUTION: It need configuration.

1) app.config works only for .NET Framework, https://github.com/dotnet/corefx/issues/24829

2) Draft for .Net Core:

TraceSource.Listeners.Add(new MyListener());
TraceSource.Switch = new SourceSwitch();
  • Trace Source show up in exception handler try/catch. You should have following : Catch(Exception e) { Console.WriteLine(e.TraceSource); } – jdweng Apr 23 '19 at 14:56

1 Answers1

1

This snippet may help you out.

public static void Main(string[] args)
{
    var webHost = new WebHostBuilder()
        .UseKestrel()
        .UseContentRoot(Directory.GetCurrentDirectory())
        .ConfigureAppConfiguration((hostingContext, config) =>
        {
            var env = hostingContext.HostingEnvironment;
            config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                  .AddJsonFile($"appsettings.{env.EnvironmentName}.json", 
                      optional: true, reloadOnChange: true);
            config.AddEnvironmentVariables();
        })
        .ConfigureLogging((hostingContext, logging) =>
        {

          logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
             logging.AddConsole();
             logging.AddDebug();
             logging.AddEventSourceLogger();
        })
        .UseStartup<Startup>()
        .Build();

    webHost.Run();
}

you can also follow this link for an in-depth guide regarding logging in dotnet core.

vzwick
  • 11,008
  • 5
  • 43
  • 63
Nitesh Shaw
  • 216
  • 2
  • 17
  • But if a referenced library uses "TraceSource" (and not knows about "ILogger") this snippet will not help out. Any other solution for that? – Marcus.D May 13 '20 at 10:15
  • Looks like they have replacement for TraceSource -- DiagnosticSource https://github.com/dotnet/runtime/blob/master/src/libraries/System.Diagnostics.DiagnosticSource/src/DiagnosticSourceUsersGuide.md – Stanislav Berkov Aug 22 '20 at 08:44