Serilog dotnet核心功能应用程序和sql接收器



我需要将dotnet5与Azure函数一起使用,因此请按照指导创建一个新的解决方案:https://learn.microsoft.com/en-us/azure/azure-functions/dotnet-isolated-process-guide.

这样做效果很好,所以下一步的工作是为控制台和sql server添加带有接收器的serilog。

我添加了nuget包:

  • 序列号。AspNetCore 4.1.0版
  • 序列号。水槽。MSSqlServer v5.6.0

这是程序。Main:

static void Main(string[] args)
{
string EventName = "Main";
var columnOptions = new ColumnOptions
{
AdditionalColumns = new Collection<SqlColumn>
{
new SqlColumn
{ColumnName = "EventName", DataType = SqlDbType.NVarChar, DataLength = 32, NonClusteredIndex = true}
}
};
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Override("Microsoft.Azure", LogEventLevel.Warning)
.Enrich.FromLogContext()
.WriteTo.Console()
.WriteTo.MSSqlServer(
logEventFormatter: new RenderedCompactJsonFormatter(),
restrictedToMinimumLevel: LogEventLevel.Debug,
connectionString: "Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=SmsRouter",
sinkOptions: new MSSqlServerSinkOptions
{
TableName = "LogEvents",
AutoCreateSqlTable = true,
},
columnOptions: columnOptions)
.CreateLogger();
try
{
Log.Information("Starting up {EventName}", EventName);
var host = new HostBuilder()
.UseSerilog()
.ConfigureFunctionsWorkerDefaults()
.ConfigureServices(s =>
{
//services configured here
})
.Build();
host.Run();
}
catch (Exception ex)
{
Log.Fatal(ex, "Application start-up failed");
}
finally
{
Log.CloseAndFlush();
}
}

您可以看到日志一行。信息("Starting up{EventName}",EventName(此操作有效,并记录到控制台和Sql服务器:(

应用程序启动后,它将等待Http请求,如下所示:

[Function("SendSimpleSms")]
public async Task<QueueAndHttpOutputType> RunSimple([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")] HttpRequestData req,
FunctionContext executionContext)
{
string EventName = "SendSimpleSms";
try
{
Log.Information("Starting: {EventName}", EventName);

我的问题是,这个日志请求";正在启动:SendSimpleSms";已记录到控制台窗口,但未记录到Sql Server。

有人看到我做错了什么吗?

感谢Panagiotis Kanavos让我了解Serilog自日志。

我在程序中添加了以下内容。main,在LoggerConfiguration之后:

Serilog.Debugging.SelfLog.Enable(Console.Error);

这让我意识到sql接收器无法记录,因为自定义属性的长度超过了其列的长度

最新更新