[英]ASP.Net Core 1 Logging Error - The description for Event ID xxxx from source Application cannot be found
我想從ASP.Net Core應用程序的Controller方法寫入Windows事件日志。
我的問題是,在我希望寫入日志信息的地方,我不斷收到錯誤/信息日志:
無法找到源應用程序中的事件ID xxxx的說明。 引發此事件的組件未安裝在本地計算機上,或者安裝已損壞。 您可以在本地計算機上安裝或修復該組件。
如果事件源自另一台計算機,則必須隨事件一起保存顯示信息。
活動中包含以下信息:
My.Fully.Qualified.Namespace.WebApi.Controllers.MyController方法'MyMethod'中出現錯誤
消息資源存在但在字符串/消息表中找不到該消息
作為一個快速說明,在.Net Core之前,我總是通過使用Powershell或Command創建事件源或正確配置Microsoft.Practices.EnterpriseLibrary.Logging應用程序塊或其他第三方庫來解決類似的錯誤
我使用的方法是:
安裝Nuget軟件包: Microsoft.Extensions.Logging.Abstractions和Microsoft.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;
}
}
我讀過並試過的內容:
我無法在.Net Core 2.0中使用loggerFactory.AddEventLog()。 由於Microsoft.Extensions.Logger.EventLog僅存在於.net 4.5.1中,盡管此處的Nuget包可用於2.0.0,因此看起來需要將其移植到Core。
但也許這對你有用。 添加您的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");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.