Log assembly specific information in dotnet core

Viewed 31

I'm trying to get MEF logging/tracing to work for my .netcore webapi. For the .netframework version I can use the following configuration to get all the traces I need in a separate file.

<configuration>
<system.diagnostics>
    <sources>
        <source name="System.ComponentModel.Composition"
                switchValue="All">
            <listeners>
                <add name="fileListener"
                     type="System.Diagnostics.TextWriterTraceListener"
                     initializeData="composition.log" />
            </listeners>
        </source>
    </sources>
    <trace autoflush="true" indentsize="4" />
</system.diagnostics>

What would be the .net core equivalent?

I tried the following in my program.cs file

    public class Program
{
    public static void Main(string[] args)
    {
        CreateWebHostBuilder(args).Build().Run();
    }

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args).ConfigureAppConfiguration((hostingContext, config) =>
            {
                config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                    .AddEnvironmentVariables();
            })
            .ConfigureLogging((hostingContext, logging) =>
            {
                logging.ClearProviders();
                logging.AddFilter("System.ComponentModel.Composition", LogLevel.Trace);
                logging.AddTraceSource("All");
                logging.AddConsole();

            })
            .UseStartup<Startup>();
}

but nothing is shown in the console and I did not find a way to log this kind of events to a file.

0 Answers
Related