gpt4 book ai didi

c# - ASP.Net Core 1 日志记录错误 - 无法找到来自源应用程序的事件 ID xxxx 的描述

转载 作者:可可西里 更新时间:2023-11-01 08:29:56 25 4
gpt4 key购买 nike

我想从 ASP.Net Core 应用程序的 Controller 方法写入 Windows 事件日志。

我遇到的问题是,在我希望写入日志信息的地方,我不断收到错误/信息日志:

The description for Event ID xxxx from source Application cannot be found. Either the component that raises this event is not installed on your local computer or the installation is corrupted. You can install or repair the component on the local computer.

If the event originated on another computer, the display information had to be saved with the event.

The following information was included with the event:

My.Fully.Qualified.Namespace.WebApi.Controllers.MyController Error occured in method 'MyMethod'

the message resource is present but the message is not found in the string/message table

快速说明一下,在使用 .Net Core 之前,我一直使用 resolved a similar error by creating the Event Source using Powershell or Command 或正确配置 Microsoft.Practices.EnterpriseLibrary.Logging Application block 或其他第三方库

我使用的方法是:

安装 Nuget 包:Microsoft.Extensions.Logging.AbstractionsMicrosoft.Extensions.Logging 后,我在 Start Configure 代码块中指定了 EventLog Provider

    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory
.AddDebug()
.AddEventLog();

//... rest of the code here
}

对于每个 Controller ,使用 ILogger<T> 方法:

public class MyController : Controller
{
private readonly IMyRepository _myRepository;
private readonly ILogger _logger;

public MyController(IMyRepository myRepository,
ILogger<MyController> logger)
{
_myRepository = myRepository;
_logger = logger;
}

public bool MyMethod(MyRequestObject request)
{
try
{
var response = privateDoSomethingMethod(request);

return response.Success;
}
catch (Exception ex)
{
_logger.LogError(6666,ex, "Error occured when doing stuff.");

return false;
}
}

这是基于official documentation on Logging

我阅读和尝试过的内容:

最佳答案

我无法在 .Net Core 2.0 中使用 loggerFactory.AddEventLog()。由于 Microsoft.Extensions.Logger.EventLog 仅存在于 .net 4.5.1 中,尽管 Nuget 包可用于 2.0.0 here , 所以看起来它需要移植到 Core。

enter image description here

但也许这对你有用。添加您的 CustomEventIds

 public static class CustomEventIds
{
public const int Debug = 100;
public const int Information = 101;
public const int Warning = 102;
public const int Error = 103;
public const int Critical = 104;
}

和:

_logger.LogInformation(CustomEventIds.Debug, "Starting to do something with input: {0}", "test");

关于c# - ASP.Net Core 1 日志记录错误 - 无法找到来自源应用程序的事件 ID xxxx 的描述,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45861604/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com