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();
1
  • Trace Source show up in exception handler try/catch. You should have following : Catch(Exception e) { Console.WriteLine(e.TraceSource); } Commented Apr 23, 2019 at 14:56

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

But if a referenced library uses "TraceSource" (and not knows about "ILogger") this snippet will not help out. Any other solution for that?
Looks like they have replacement for TraceSource -- DiagnosticSource github.com/dotnet/runtime/blob/master/src/libraries/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.