AndreyMagnificent
AndreyMagnificent

Reputation: 175

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

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();

Upvotes: 5

Views: 2316

Answers (1)

Nitesh Shaw
Nitesh Shaw

Reputation: 216

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.

Upvotes: 1

Related Questions