简体   繁体   English

通过Serilog在Azure上记录ASP.NET Core

[英]Logging ASP.NET Core on Azure via Serilog

I have a ASP.NET Core 2.x Azure web app. 我有一个ASP.NET Core 2.x Azure Web应用程序。 I'm using Serilog to handle the logging. 我正在使用Serilog来处理日志记录。 I would like my log files to end up in Azure Blob Storage but have been unable to get it configured properly. 我希望我的日志文件最终在Azure Blob存储中,但无法正确配置。

As per the instructions at this link , I have enabled "Blob Application Logging" with minimum level "Information". 根据此链接中的说明,我已启用“Blob应用程序日志记录”,其中包含最低级别的“信息”。

In my web app's Startup.cs file, I have configured Serilog as follows: 在我的Web应用程序的Startup.cs文件中,我按如下方式配置了Serilog:

Serilog.Log = new LoggerConfiguration()               
               .WriteTo.Trace(Serilog.Events.LogEventLevel.Information)
               .WriteTo.Console(Serilog.Events.LogEventLevel.Information)
            .WriteTo.RollingFile(@"../logs/logfile-{Date}.txt", outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} {Level}:{EventId} [{SourceContext}] {Message}{NewLine}{Exception}")
            .CreateLogger();

With this configuration, the local file system rolling file sink seems to work just fine, but I'm not getting any logs from Serilog in the blob storage files. 使用此配置,本地文件系统滚动文件接收器似乎工作正常,但我没有从blob存储文件中的Serilog获取任何日志。

I do get SOME logs in the blob storage logs, but none of them are from Serilog, they all seem to come from the .NET framework itself. 我确实在blo​​b存储日志中获得了一些日志,但它们都不是来自Serilog,它们似乎都来自.NET框架本身。

Am I missing some kind of link that would allow me to pipe Serilog output to the blob storage logs along with the Microsoft logs? 我错过了某种链接,允许我将Serilog输出与Microsoft日志一起传递给blob存储日志吗?

The Serilog.Sinks.AzureApp sink is designed for this - it uses the settings in the "diagnostic logs" section of the Azure portal. Serilog.Sinks.AzureApp器专为此而设计 - 它使用Azure门户的“诊断日志”部分中的设置。 See its github page for more details, and also the discussion here . 有关更多详细信息,请参阅其github页面 ,以及此处讨论

To write logs to Azure Storage, we need to install the package of Serilog.Sinks.AzureTableStorage . 要将日志写入Azure存储,我们需要安装Serilog.Sinks.AzureTableStorage包。 We can install it via NuGet as below: 我们可以通过NuGet安装它,如下所示:

在此输入图像描述

And we can do in our code using method WriteTo.AzureTableStorage , which looks like below: 我们可以使用方法WriteTo.AzureTableStorage在我们的代码中WriteTo.AzureTableStorage ,如下所示:

在此输入图像描述

For Serilog, there is no sinks for azure blob from it's offical site here . 对于Serilog,对蔚蓝的BLOB没有汇从它的官方网站在这里

I find a third-party sink for azure blob named Mike.Serilog.Sinks.AzureStorage, which can write Serilog log messages to a specifid blob(it's a different blob from the blob stored .net framework log), here . 我找到了一个名为Mike.Serilog.Sinks.AzureStorage的azure blob的第三方接收器,它可以将Serilog日志消息写入特定的blob(它与blob存储的.net框架日志不同的blob), 这里

If you like to have a try, please follow the steps below: 如果您想尝试一下,请按照以下步骤操作:

step 1: Download the project from here . 第1步:从这里下载项目。

step 2: Open the RollingAzureBlobSink.cs -> in line 62, use this line of code await _blob.AppendTextAsync(evnt.MessageTemplate.Text); 步骤2:打开第62行的RollingAzureBlobSink.cs - >,使用这行代码await _blob.AppendTextAsync(evnt.MessageTemplate.Text); to replace the original code. 替换原始代码。 Then build the project. 然后构建项目。 在此输入图像描述

step 3: Create a new ASP.NET Core 2.0 project, and add the reference "Mike.Serilog.Sinks.AzureStorage.dll" in step 2. 步骤3:创建一个新的ASP.NET Core 2.0项目,并在步骤2中添加引用“Mike.Serilog.Sinks.AzureStorage.dll”。

step 4: Add the following code in the Starup.cs, method public Startup(IConfiguration configuration) as following: 步骤4:在Starup.cs中添加以下代码,方法public Startup(IConfiguration配置)如下:

public Startup(IConfiguration configuration)
        {
            Configuration = configuration;

            StorageCredentials credentials = new StorageCredentials("your storage account", "your key");
            CloudStorageAccount storageAccount = new CloudStorageAccount(credentials, false);

            Log.Logger = new LoggerConfiguration()
                .MinimumLevel.Debug()
                .WriteTo.Trace(Serilog.Events.LogEventLevel.Information)
                .WriteTo.Console(Serilog.Events.LogEventLevel.Debug)
                .WriteTo.RollingFile(@"your file path", outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} {Level}:{EventId} [{SourceContext}] {Message}{NewLine}{Exception}")
                .WriteTo.RollingAzureBlobSink(null, storageAccount, "testyy", "jj", 5, TimeSpan.FromMinutes(3))
                .CreateLogger();

            Log.Debug("a good thing debug");
            Log.Information("a info inforxxxxx");
        }

step 5: Publish to azure, and remember to enable "Blob Application Logging" with minimum level "Information". 步骤5:发布到azure,并记住启用具有最低级别“信息”的“Blob应用程序日志记录”。

step 6: Run the website, and you can see in your storageAccouont -> container(here is testyy), a log starts with jj are created, and Serilog are written into it. 第6步:运行网站,你可以在你的storageAccouont - >容器(这里是testyy)中看到,创建一个以jj开头的日志,并将Serilog写入其中。 在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM