我正在尝试登录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日志记录文档.
更新:如果有人仍然通过.NET Core 2.2登陆此处,则这仍然与.NET Core 3.1日志记录.
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中的事件查看器
发布评论