gpt4 book ai didi

c# - Serilog 不使用 Serilog.Sinks.MssqlServer 将日志写入 SQL Server

转载 作者:行者123 更新时间:2023-12-05 01:54:01 24 4
gpt4 key购买 nike

我正在尝试使用 Serilog 设置日志记录机制。我想将日志写入文件和 SQL Server 数据库。

目前我可以将日志写入文件系统,但无法写入数据库。我还进行了与 Serilog 文档中相同的简单设置

谢谢。

public class Program
{
public static void Main(string[] args)
{
var logDB = @"Server=localhost;Initial Catalog=SHARED_NOTE;User ID=sa;Password=sql123;";
var sinkOpts = new MSSqlServerSinkOptions();
sinkOpts.TableName = "Logs";
var columnOpts = new ColumnOptions();
columnOpts.Store.Remove(StandardColumn.Properties);
columnOpts.Store.Add(StandardColumn.LogEvent);
columnOpts.LogEvent.DataLength = 2048;
columnOpts.TimeStamp.NonClusteredIndex = true;

Log.Logger = new LoggerConfiguration()
.WriteTo.File(new CompactJsonFormatter(), "Log.json", rollingInterval: RollingInterval.Day)
.WriteTo.Console(restrictedToMinimumLevel:Serilog.Events.LogEventLevel.Information)
.WriteTo.MSSqlServer(
connectionString: logDB,
sinkOptions: sinkOpts,
columnOptions: columnOpts
)
.CreateLogger();

try
{
Log.Information("Application starting up.");

CreateHostBuilder(args).Build().Run();
}
catch (Exception ex)
{
Log.Fatal(ex, "The application failed to start up correctly.");
}
finally
{
Log.CloseAndFlush();
}
}

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.UseSerilog()
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}

我将 AddLogging 添加到 startup.cs:

services.AddLogging();

所有包都是为 Web API 项目设置的:

<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentValidation" Version="10.3.6" />
<PackageReference Include="FluentValidation.AspNetCore" Version="10.3.6" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="5.0.12">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Serilog.AspNetCore" Version="4.1.0" />
<PackageReference Include="Serilog.Formatting.Compact" Version="1.1.0" />
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
<PackageReference Include="Serilog.Sinks.MSSqlServer" Version="5.6.1" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.6.3" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\Core\SharedNote.Application\SharedNote.Application.csproj" />
<ProjectReference Include="..\..\Infrastructure\SharedNotes.Persistence\SharedNotes.Persistence.csproj" />
</ItemGroup>

<ItemGroup>
<Folder Include="wwwroot\Images\" />
<Folder Include="wwwroot\Docs\" />
</ItemGroup>

</Project>

最佳答案

没有足够的评论评论,你试过关注这篇文章吗? Serilog log to SQL .

您还没有添加日志记录表,但我假设您关注了 Sink,它与这个相似或匹配吗?

TABLE [Log] (

[Id] int IDENTITY(1,1) NOT NULL,
[Message] nvarchar(max) NULL,
[MessageTemplate] nvarchar(max) NULL,
[Level] nvarchar(128) NULL,
[TimeStamp] datetimeoffset(7) NOT NULL,
[Exception] nvarchar(max) NULL,
[Properties] xml NULL,
[LogEvent] nvarchar(max) NULL

CONSTRAINT [PK_Log]
PRIMARY KEY CLUSTERED ([Id] ASC)

)

此外,在同一篇文章中,您可以在 Logger 设置之后添加以下代码以调试 SQL 连接

Serilog.Debugging.SelfLog.Enable(msg =>
{
Debug.Print(msg);
Debugger.Break();
});

所以在你的代码中它会是

Log.Logger = new LoggerConfiguration()
.WriteTo.File(new CompactJsonFormatter(),
"Log.json",
rollingInterval: RollingInterval.Day)
.WriteTo.Console(restrictedToMinimumLevel:Serilog.Events.LogEventLevel.Information)
.WriteTo.MSSqlServer(
connectionString: logDB,
sinkOptions: sinkOpts,
columnOptions: columnOpts
)
.CreateLogger();

Serilog.Debugging.SelfLog.Enable(msg =>
{
Debug.Print(msg);
Debugger.Break();
});

关于c# - Serilog 不使用 Serilog.Sinks.MssqlServer 将日志写入 SQL Server,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70869694/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com