Logging in .Net core console application not working

前端 未结 3 1867
孤街浪徒
孤街浪徒 2021-01-02 19:07

I am following this tutorial: https://andrewlock.net/using-dependency-injection-in-a-net-core-console-application/

and accordingly installed the packages but log is

3条回答
  •  滥情空心
    2021-01-02 19:44

    Creating a new ServiceProvider and HostBuilder may not be worth it if we just want a Logging in Console Application because it's a bit of extra caution to clean it up or dispose of. Rather, I would suggest just have Logging Factory to use logger and that will solve the logging if that is only what we want.

    public static class ApplicationLogging
    {
        public static ILoggerFactory LogFactory { get; } = LoggerFactory.Create(builder =>
            builder.ClearProviders(); 
            // Clear Microsoft's default providers (like eventlogs and others)
            builder.AddSimpleConsole(options =>
            {
                options.IncludeScopes = true;
                options.SingleLine = true;
                options.TimestampFormat = "hh:mm:ss ";
            });
            builder.AddApplicationInsights("instrument-key");
        });
        public static ILogger CreateLogger() => LogFactory.CreateLogger();
    }
    
    static void Main(string[] args)
    {
        var logger = ApplicationLogging.CreateLogger();
        logger.LogInformation("Let's do some work");
        logger.LogWarning("I am going Crazy now!!!");
        logger.LogInformation("Seems like we are finished our work!");
        Console.ReadLine();
    }
    

提交回复
热议问题