asp.net-core - 为什么 EventLog 提供程序在事件查看器中没有出现任何条目?
问题描述
我在 Windows 10 Enterprise x64 机器上使用非托管应用程序池将 dotnet core 3.1 Web API 发布到 IIS。
我正在尝试像这样配置 EventLog 提供程序...
.ConfigureLogging(logging =>
{
logging.ClearProviders();
logging.AddEventLog(new EventLogSettings { LogName = "Web API ABC"
}
...但是当我这样做时,事件查看器中没有出现任何内容logger.LogInformation("can you see me?");
这是appsettings.json
,program.cs
和Controller
应用设置.json
{
"Logging": {
"LogLevel": {
"Default": "Debug",
"Microsoft": "Debug",
"Microsoft.Hosting.Lifetime": "Debug"
}
},
"EventLog": {
"LogLevel": {
"Default": "Debug"
}
}
}
程序.cs
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureLogging(logging =>
{
logging.ClearProviders();
logging.AddConsole();
logging.AddEventLog(new Microsoft.Extensions.Logging.EventLog.EventLogSettings { LogName = "Web API ABC" });
})
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
控制器:
[ApiController]
[Route("[controller]")]
public class MyGreatController : ControllerBase
{
private IConfiguration config;
private readonly ILogger<MyGreatController> logger;
public MyGreatController(IConfiguration _config, ILogger<MyGreatController> _logger)
{
config = _config;
logger = _logger;
}
[HttpGet]
[Route("hello")]
public string HelloWorld()
{
logger.LogInformation("can you see me?");
return "hello world";
}
}
解决方案
您需要在Web API ABC
运行应用程序的服务器上创建自定义事件日志源(一次性设置),然后才能将日志写入其中。
以下是使用 PowerShell 创建事件日志源的示例:
$ErrorActionPreference = "Stop"
$sourceExists = [System.Diagnostics.Eventlog]::SourceExists("Web API ABC")
if ($sourceExists -eq $false){
[System.Diagnostics.EventLog]::CreateEventSource("Web API ABC", "Application")
}
推荐阅读
- python - 个人项目代码问题(替换字符)
- python - 让 Selenium 从 .txt 文件中获取 URL 列表
- php - 如何根据while循环和数据库添加复选框
- react-native - 如何在 Listview 组件中使用条件?
- javascript - 如何在javascript中将对象数组附加到卡片元素
- c++ - extern 模板实际上是如何生成代码的?
- git - git hook not behaving consistently between linux and windows
- python - 如何删除标题,转置数据并将 100 个文件组合成一个新的 csv 文件?
- javascript - Reactjs hiding and displaying start screen by using hooks instead of classes
- phpmyadmin - xamp port 8080 server