我有ASP.NET Core web api项目.现在我想记录错误和事件.我之前在我的项目中使用过ELMAH,但似乎elmah不适用于ASP.NET Core.我提到了 这个官方链接, 用于配置Microsoft提供的默认日志记录服务.我看不到如何将这些日志保存在文本文件或数据库中.
如果ASP.NET Core已经具有默认日志记录功能,我想知道为什么我应该使用其他工具,如elmah,log4net.因此,当我搜索实现默认日志记录以将日志保存在db或文本文件中的文章时,我找不到任何内容.有没有什么方法可以使用ASP.NET核心内置的日志记录支持将日志保存到文件中?
我目前正在使用Serilog,它运行良好,并且还下载了seq,用于在浏览器中优雅地显示日志.但是,我仍然想知道如何使用内置的asp.net核心日志记录功能实现相同的功能.
logging serilog entity-framework-core asp.net-core asp.net-core-logging
Startup由于为 Web 主机创建了单独的 DI 容器,因此在早期版本的 ASP.NET Core 中可以使用构造函数注入记录器。截至目前,仅为通用主机创建了一个容器,请参阅重大变更公告。
启动.cs
public class Startup
{
/// <summary> The configuration. </summary>
public IConfiguration Configuration { get; }
/// <summary> The web host environment. </summary>
public IWebHostEnvironment WebHostEnvironment { get; }
public Startup(IConfiguration configuration, IWebHostEnvironment webHostEnvironment)
{
Configuration = configuration;
WebHostEnvironment = webHostEnvironment;
}
public void ConfigureServices(IServiceCollection services)
{
services.AddServices(Configuration); // This is a custom method, that adds multiple services to the container.
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILogger<Startup> …Run Code Online (Sandbox Code Playgroud) 最近我配置了Serilog以与ASP.NET Core 2 MVC应用程序配合使用,接下来的任务是在系统的每一层上跟踪传入的Web应用程序请求.所以基本上,我们想要传播一些令牌(比如RequestId由Serilog生成到较低层的应用程序).
"Serilog": {
"Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.RollingFile" ],
"MinimumLevel": {
"Default": "Debug",
"Override": {
"Microsoft": "Warning"
}
},
"WriteTo": [
{
"Name": "RollingFile",
"Args": {
"pathFormat": "log-{Hour}.txt",
"fileSizeLimitBytes": "",
"retainedFileCountLimit": "",
"outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Application}] [{Level}] [{RequestId}] - {Message}{NewLine}{Exception}"
}
}
],
"Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId" ],
"Properties": {
"Application": "MultilayerApp"
}
},
Run Code Online (Sandbox Code Playgroud)
在日志中,我们有很好的输入
2018-01-19 14:06:01.165 +00:00 [App] [Warning] [0HLAV6PIMA9M0:00000001] - Accessing expired session, Key:"6e7f3ab5-db62-335d-1bc7-6824e5b918f5"
Run Code Online (Sandbox Code Playgroud)
但我的问题是Serilog在哪里实现更丰富RequestId?老实说,我找不到它.
我在Azure中部署了一个ASP.NET Core Web项目,并配置了Application Insights.Insights正在接收请求等的数据,但我无法显示我的日志.
我正在使用vanilla Microsoft.Extensions.Logging框架并在控制器操作上记录测试错误
logger.LogError("Test application insights message");
Run Code Online (Sandbox Code Playgroud)
在我设置的Startup.cs配置方法中
loggerFactory.AddAzureWebAppDiagnostics();
Run Code Online (Sandbox Code Playgroud)
...并且可以成功查看Azure日志流中出现的错误消息:
2017-04-14 11:06:00.313 +00:00 [Error] Test application insights message
Run Code Online (Sandbox Code Playgroud)
但是,我还希望此消息显示在Application Insights跟踪中,但无处可寻.我想我需要配置ILoggerFactory但不确定如何找到关于这个主题的文档.
在此先感谢您的帮助
azure-application-insights asp.net-core asp.net-core-logging
在 blazor 应用程序中,我在main中配置了启动日志记录,如下所示;
public static void Main(string[] args)
{
var assembly = Assembly.GetExecutingAssembly().GetName();
var appInsightsTelemetryConfiguration = TelemetryConfiguration.CreateDefault();
appInsightsTelemetryConfiguration.InstrumentationKey = "aa11aa11aa-a1a1-a1-aa-a111-aa11";
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Override("Microsoft.AspNetCore", Serilog.Events.LogEventLevel.Warning)
.Enrich.FromLogContext()
.Enrich.WithProperty("Application", $"{assembly.Name}")
.WriteTo.Console()
.WriteTo.Seq(serverUrl: "http://myseqserver.inthecloud.azurecontainer.io:5341/")
.WriteTo.ApplicationInsights(appInsightsTelemetryConfiguration, TelemetryConverter.Traces)
.CreateLogger();
try
{
Log.Information(Constants.Logging.Messages.SERVICE_STARTED, assembly.Name);
var host = CreateHostBuilder(args).Build();
using (var serviceScope = host.Services.CreateScope())
{
var context = serviceScope.ServiceProvider.GetRequiredService<MyDbContext>();
context.Database.Migrate(); // apply outstanding migrations automatically
}
host.Run();
return;
}
catch (Exception ex)
{
Log.Fatal(ex, Constants.Logging.Messages.SERVICE_STARTED, assembly.Name);
return;
}
finally
{
// make sure all batched …Run Code Online (Sandbox Code Playgroud) serilog asp.net-core blazor asp.net-core-logging blazor-server-side
事实证明,该问题与
NLogASP.NET日志记录系统无关。日志配置正确,但是Debug由于配置错误,构建服务器正在将构建发布到生产中。
我试图在我的ASP.NET Core 2.0(SDK 2.1.401)项目中设置第三方记录器。到目前为止,我曾尝试Serilog和NLog。但是我对两个都有同样的问题。总结一下问题,
dotnet run(例如,在Development环境中)dotnet MyApplication.dll(即在Production环境中)却没有。"Logging"为Development环境部分定义任何内容appsettings.Development.json。下面描述的问题是针对的NLog。我尝试过SeriLog并遇到相同的问题。
这是来自的相关部分 Program.cs
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseApplicationInsights()
.UseStartup<Startup>()
.ConfigureLogging((env, logging) =>
{
logging.ClearProviders();
logging.SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Trace);
})
.UseNLog()
.Build();
Run Code Online (Sandbox Code Playgroud)
注意我已经清除了所有提供程序并添加NLog了nlog.config文件定义:
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseApplicationInsights()
.UseStartup<Startup>()
.ConfigureLogging((env, logging) =>
{
logging.ClearProviders();
logging.SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Trace);
}) …Run Code Online (Sandbox Code Playgroud)