惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

云风的 BLOG
云风的 BLOG
V
Visual Studio Blog
人人都是产品经理
人人都是产品经理
The GitHub Blog
The GitHub Blog
月光博客
月光博客
T
Tailwind CSS Blog
小众软件
小众软件
Y
Y Combinator Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
P
Proofpoint News Feed
B
Blog RSS Feed
博客园 - 司徒正美
A
About on SuperTechFans
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园 - 聂微东
Microsoft Security Blog
Microsoft Security Blog
Recent Announcements
Recent Announcements
博客园 - Franky
U
Unit 42
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Microsoft Azure Blog
Microsoft Azure Blog
T
The Blog of Author Tim Ferriss
GbyAI
GbyAI
Apple Machine Learning Research
Apple Machine Learning Research

博客园 - 网络来者

Element Plus 多页面后台与 ASP.NET Core API 从零手敲教程 从零创建 MESTOOLS 消息弹窗跟练教程 uni app开发一个子页面 MES物料分摊 墨西哥 嘉兴 兼容车牌摄像头 和 tplink 摄像头的sdk开发包 集成 训练数字识别模型 tplink摄像头监控 mes测试机 redis vue学习 vue 系统文件结构 udp server 监听服务 第一个服务 c# 开发相关 openclaw 安装 c# 版本号 oracle 触发器 脚本方式安装Python 特定版本 idea 激活 Chrome MCP Server mcp ok mcp_server RepositoryItemGridLookUpEdit 使用 ok devexpress gridcontrol表格知识 winfrom 弹文本框 松下贴片机解锁 git 使用
Serilog日志组件使用
网络来者 · 2025-08-20 · via 博客园 - 网络来者

builder.Logging.ClearProviders(); // 移除默认的日志提供程序 Log.Logger = new LoggerConfiguration() .MinimumLevel.Debug() .MinimumLevel.Override("Microsoft.AspNetCore.Hosting", LogEventLevel.Warning) .MinimumLevel.Override("Microsoft.AspNetCore.Mvc", LogEventLevel.Warning) .MinimumLevel.Override("Microsoft.AspNetCore.Routing", LogEventLevel.Warning) .WriteTo.Console() .WriteTo.Elasticsearch(new ElasticsearchSinkOptions(new Uri("http://localhost:9200")) { // 基本配置 AutoRegisterTemplate = true, // 自动创建/更新索引模板 (适用于 ES 7.x/8.x) AutoRegisterTemplateVersion = AutoRegisterTemplateVersion.ESv7, // 指定模板版本 (ESv8 for ES 8.x, ESv7 for ES 7.x) IndexFormat = "logs-{0:yyyy.MM.dd}", // 索引名称格式,按天滚动 TemplateName = "serilog-events-template", // 模板名称 NumberOfShards = 1, // 分片数 NumberOfReplicas = 1, // 副本数 // 批处理配置 BatchPostingLimit = 1000, // 每批发送的最大日志数 Period = TimeSpan.FromSeconds(5), // 批处理间隔时间 // 连接和重试配置 ConnectionTimeout = TimeSpan.FromSeconds(10), // EmitLogEvent = true, // 是否发送完整的 LogEvent 对象 (默认 true) // 认证 (如果 Elasticsearch 需要) // 如果你的 Elasticsearch 有用户名密码 // ModifyConnectionSettings = connectionConfiguration => connectionConfiguration // .BasicAuthentication("your-username", "your-password"), // 如果是 HTTPS 且需要忽略证书验证 (仅限开发环境!) // ModifyConnectionSettings = connectionConfiguration => connectionConfiguration // .ServerCertificateValidationCallback((sender, certificate, chain, errors) => true), // (可选) 如果需要配置特定的序列化或日志属性 // CustomFormatter = new ElasticsearchJsonFormatter(), // 使用默认或自定义格式化器 }) .WriteTo.MSSqlServer(connectionString: "Server=localhost;Database=LogDb;Integrated Security=SSPI;Encrypt=True; TrustServerCertificate=True;" , sinkOptions: new MSSqlServerSinkOptions { TableName = "LogEvents", AutoCreateSqlTable = true }) .WriteTo.File("Logs/log-.txt", rollingInterval: RollingInterval.Day, outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj}{NewLine}{Exception}") .CreateLogger(); builder.Host.UseSerilog(dispose: true);

  app.UseSerilogRequestLogging(); //一定不能少这一句