登录到ASP .Net Core 2.1 Web API中的事件查看器 [英] Log to Event Viewer in an ASP .Net Core 2.1 Web API

查看:105
本文介绍了登录到ASP .Net Core 2.1 Web API中的事件查看器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试登录Windows Server 2016 Standard上托管的ASP .Net Core 2.1 Web API中的事件查看器.

I'm trying to log to the Event Viewer in an ASP .Net Core 2.1 Web API, hosted on Windows Server 2016 Standard.

我已经在控制器中找到了它:

I've got this in my controller:

private readonly ILogger<MyController> _logger;
private readonly MyContext _context;

public TestController(MyContext context, ILogger<MyController> logger)
{
    _context = context;
    _logger = logger;
}

但是我认为我在Program.cs的CreateWebHostBuilder()方法中做错了,因为它不起作用:

But I think I'm doing something wrong in my CreateWebHostBuilder() method in Program.cs because it's not working:

我有这个:

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>();

我将其修改为:

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseStartup<Startup>()
            .ConfigureLogging((hostingContext, logging) =>
            {
                logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
                logging.AddEventSourceLogger();
            });

但是我一定做错了什么...有什么主意吗?我在这里阅读到,显然EVent Viewer日志记录现在已经嵌入到.Net Core 2.1中.写入EventLog .Net Core

But I must be doing something wrong... Any ideas? I read here that apparently EVent Viewer logging is now baked into .Net Core 2.1 Write to EventLog in .Net Core

推荐答案

对于使用.NET Core 2.2偶然发现此问题的任何人,事件日志记录器(来自 Microsoft.Extensions.Logging.EventLog )现在应该添加到Program.cs:

For anyone who stumbles upon this using .NET Core 2.2, the Event Log logger (from Microsoft.Extensions.Logging.EventLog) should now be added in the Program.cs:

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

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
    WebHost.CreateDefaultBuilder(args)
        .ConfigureLogging((context, logging) =>
        {
            logging.AddEventLog();
        })
        .UseStartup<Startup>();

这在 更新:如果有人仍然通过.NET Core 2.2登陆此处,则这仍然与

UPDATE: In case someone still lands here post .NET Core 2.2, this is still relevant for .NET Core 3.1 logging.

这篇关于登录到ASP .Net Core 2.1 Web API中的事件查看器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆