登录到ASP .Net Core 2.1 Web API中的事件查看器

编程入门 行业动态 更新时间:2024-10-26 00:26:40
本文介绍了登录到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日志记录文档.

更新:如果有人仍然通过.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中的事件查看器

本文发布于:2023-11-16 20:59:31,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1607434.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:查看器   事件   Core   Net   ASP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!