58 lines
1.8 KiB
C#
58 lines
1.8 KiB
C#
using Serilog;
|
|
using Serilog.Sinks.Grafana.Loki;
|
|
using System;
|
|
using YuanXuan.IM.Api.CollectionExtensions;
|
|
|
|
namespace YuanXuan.IM.Api.CollectionExtensions
|
|
{
|
|
public static class SerilogServiceCollectionExtensions
|
|
{
|
|
/// <summary>
|
|
/// 添加Serilog
|
|
/// </summary>
|
|
/// <param name="services"></param>
|
|
/// <param name="configuration"></param>
|
|
/// <returns></returns>
|
|
public static IServiceCollection AddSerilog(this IServiceCollection services, IConfiguration configuration, IHostBuilder hostBuilder, IHostEnvironment environment)
|
|
{
|
|
var labels = new List<LokiLabel>
|
|
{
|
|
new LokiLabel
|
|
{
|
|
Key="app",
|
|
Value=environment.ApplicationName
|
|
},
|
|
new LokiLabel
|
|
{
|
|
Key="env",
|
|
Value=environment.EnvironmentName
|
|
},
|
|
};
|
|
|
|
// 配置 Serilog
|
|
var logger = new LoggerConfiguration()
|
|
.WriteTo.Console()
|
|
.Enrich.FromLogContext();
|
|
|
|
//if (environment.IsDevelopment())
|
|
//{
|
|
logger.MinimumLevel.Information()
|
|
.WriteTo.GrafanaLoki(configuration["GrafanaLoki:LokiUri"], labels, new List<string>()
|
|
{
|
|
//"RequestId","Path"
|
|
}, tenant: configuration["GrafanaLoki:TenantId"]);
|
|
|
|
//}
|
|
//else
|
|
//{
|
|
// logger.MinimumLevel.Information()
|
|
// .WriteTo.GrafanaLoki(configuration["GrafanaLoki:LokiUri"], labels);
|
|
//}
|
|
|
|
Log.Logger = logger.CreateLogger();
|
|
services.AddSerilog(Log.Logger);
|
|
return services;
|
|
}
|
|
}
|
|
}
|