我正在尝试在 .Net Core API 中实现日志记录,但在任何地方都看不到日志文件。
我正在使用 Microsoft.Extensions.Logging;和 ILogger 工厂方法。
在我的 Program.cs 中我有...
var host = new WebHostBuilder()
.UseApplicationInsights()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.Build();
host.Run();
Run Code Online (Sandbox Code Playgroud)
在我的 Startup.cs 中,我在配置方法中有这个...
loggerFactory.AddApplicationInsights(app.ApplicationServices, LogLevel.Warning);
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
loggerFactory.AddEventSourceLogger();
Run Code Online (Sandbox Code Playgroud)
在我的 appsettings.json 中我有这个...
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
}
Run Code Online (Sandbox Code Playgroud)
在我的控制器中,我注入记录器并以这种方式调用该方法......
_logger.LogError("ERROR_MESSAGE_STRING");
Run Code Online (Sandbox Code Playgroud)
正在使用错误字符串调用 LogError 方法,但我没有看到日志。我希望在某个地方看到日志错误文件。也许在 bin 目录中?
我有一个要测试的 API。这是一种方法的签名......
[HttpGet("{param}")]
public async Task<IActionResult> Get(string param, string param2)
{
...
}
Run Code Online (Sandbox Code Playgroud)
在测试项目中,我以这种方式构建调用...
HttpClient client = new HttpClient();
string uri = "http://localhost:63779/api/controller_name/param/";
HttpResponseMessage response = await client.GetAsync(uri);
Run Code Online (Sandbox Code Playgroud)
param 是路线的一部分,但如何将 param2 带到该方法?
我有一个包含两个项目的解决方案,一个在 Framework 中,另一个在 Core 中。他们彼此互动。但是即使我安装了这个 Nuget 库,我还是在 Framework 项目中收到了这个错误。我注意到当我删除旧版本 (System.Configuration) 时,此代码将无法编译...
ConfigurationManager.ConnectionStrings["NAME"].ConnectionString;
Run Code Online (Sandbox Code Playgroud)
这告诉我它无论如何都忽略了“System.Configuration.ConfigurationManager”。但是,如果我同时安装了它,则会在运行时出现上述错误。为什么框架项目甚至需要这个库?这是一个核心库,不是吗?
我应该指出,Framework 项目是一个类库。