Can I use TraceSource in ASP .Net Core for logging?

2.1k Views Asked by At

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

There are 1 best solutions below

2
Nitesh Shaw On BEST ANSWER

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.