public static LoggerConfiguration BuildLoggerConfiguration() { string templateWithSource = "{Timestamp:HH:mm:ss.fff} [{Level:u3}] {SourceContext} {CorrelationId} {Message}{NewLine}{Exception}"; var loggerConfiguration = new LoggerConfiguration(); var aliLogConfig = new AliLogConfig() { AccessKey = "accessKeyId", AccessKeySecret = "accessKey", Endpoint = "https://cn-qingdao.log.aliyuncs.com", Project = "serilog-aliyunlog-test", LogStoreName = "LogStoreName", }; if (aliLogConfig.IsValid()) { var aliLogClient = GetAliLogServiceClient(aliLogConfig); loggerConfiguration.WriteTo.AliyunLog(aliLogClient, aliLogConfig.LogStoreName, aliLogConfig.Project, outputTemplate: templateWithSource); Console.WriteLine("加载阿里云日志配置成功"); } else { Console.WriteLine("无效的阿里云日志配置文件"); } return(loggerConfiguration); }
public static HttpLogServiceClient GetAliLogServiceClient(AliLogConfig config) { var aliLogClient = LogServiceClientBuilders .HttpBuilder .Endpoint(config.Endpoint, config.Project) .Credential(config.AccessKey, config.AccessKeySecret) .Build(); return(aliLogClient); }