

在Winform项目和Web API的.NetCore项目中使用Serilog 来记录日志信息
source link: https://www.cnblogs.com/wuhuacong/p/15723774.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

在我们常规的调试或者测试的时候,喜欢把一些测试信息打印在控制台或者记录在文件中,对于.netframework项目来说,我们输出控制台的日志信息习惯的用Console.WriteLine来输出查看,不过对于.netcore的项目来说,这种输出看不到任何信息了,不过即使这样,我们建议还是把一些测试的日志记录在文件中,供查看对比。本篇随笔介绍使用Serilog 来记录日志信息,包括应用于.netframework项目和.netcore项目中都适合。
1、在Winform项目中整合Serilog 记录日志
在.NET使用日志框架第一时间会想到NLog或是Log4Net,Serilog 是这几年快速崛起的Log框架之一,Serilog是以Structured logging 为基础进行设计,通过logging API 可以轻松的记录应用程序中对象属性,方便快速进行logging 内容进行查询与分析,并将其纪录内容通过json (可指定) 的方式输出。
不管对于.netframwork或者.netcore的Winform项目来说,使用Serilog 来记录日志信息的操作都很类似,它都支持的。
官网地址:https://serilog.net/,或者参考它的Github地址:https://github.com/serilog/serilog
我们只需要引入对应的几个Serilog 相关DLL即可,如下所示。
然后在Main函数中添加对应的处理代码,如下所示。
static class Program { /// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main() { Log.Logger = new LoggerConfiguration() .MinimumLevel.Debug() .Enrich.FromLogContext() .WriteTo.Console() .WriteTo.File("logs/log.txt", rollingInterval: RollingInterval.Day) .CreateLogger();
在类代码或者界面代码中,直接通过方法输出日志信息即可。
//使用Serilog记录日志信息 Serilog.Log.Information(json);
它和其他Log组件,如Log4net一样,它的使用区分类别来输出不同类型的日志,支持Verbose、Debug、Information、Error、Warning、Fatal 几个接口的输出。
Serilog.Log.Verbose("verbose"); // 用于跟踪信息和调试细节,不会输出显示出来 //以下为常用记录日志方法 Serilog.Log.Information("info"); Serilog.Log.Debug("debug"); Serilog.Log.Warning("warning"); Serilog.Log.Error("err"); Serilog.Log.Fatal("fatal");
输出日志信息如下所示。
Serilog 如果想个性化设置日志输出到什么地方,需要引用一些serilog的接收器。Serilog的输出对象称之为Sink
Github上提供了大量的Serilog 第三方的可用sinks,这里简单的列举几个常用的:
2、在.NetCore项目的Web API中整合Serilog
在.netcore的Web API项目中配置和上面的有一些差异,引入相关的Serilog组件类库,如下所示。
其中需要在IHostBuilder或者IWebHostBuilder中注入Serilog接口,如下初始化代码所示。
在Web API的控制器中使用代码如下所示
/// <summary> /// 用于接收长语音信息回调的处理 /// </summary> [ApiController] [Route("[controller]")] public class LongVoiceController : ControllerBase { private readonly ILogger<LongVoiceController> _logger; public LongVoiceController(ILogger<LongVoiceController> logger) { _logger = logger; } /// <summary> /// 测试控制器,路由:get /longvoice /// </summary> /// <returns></returns> [HttpGet] public List<string> TestList() { return new List<string>() { "测试列表接口", "检查控制器正常" }; } /// <summary> /// 回调写入结果, 路由:post /longvoice /// </summary> /// <param name="input"></param> /// <returns></returns> [HttpPost] public async Task<CommonResultDto> CallBack(LongVoiceResultDto input) { CommonResultDto result = null; var json = JsonConvert.SerializeObject(input, Formatting.Indented); _logger.LogInformation(json); result = new CommonResultDto(0, "操作成功"); return await Task.FromResult(result); } }
我们注意到直接使用web API 的 IIogger<T>接口调用写日志即可。
_logger.LogInformation(json);
这样我们可以看到相关的日志输出
Recommend
-
32
Logging information in .NET, or really in any production application, is invaluable. In many cases, developers don’t have direct access to the production environment to debug issues. Good quality logs are the difference b...
-
25
为什么是serilog?# Serilog是 .NET 中最著名的结构化日志类库。 基于日志事件log events,而不是日志消息log message。 你可以将日志事件格...
-
53
Serilog 自定义 Enricher 来增加记录的信息 Intro Serilog 是 .net 里面非常不错的记录日志的库,结构化日志记录,而且配置起来很方便,自定义扩展也很方便 Serilog is a di...
-
41
这是Serilog系列的第三篇文章。 第1部分-使用Serilog RequestLogging减少日志详细程度
-
12
本文转载自微信公众号「码农读书」,作者码农读书 。转载本文请联系码农读书公众号。 记录日志的一个作用就是方便对应用程序进行跟踪和...
-
13
前面我们了解了 .NET Worker Service 的入门知识[1] 和
-
6
Serilog 是我在撰寫 .NET 應用程式時唯一推薦的 Logging 工具,基本上 Serilog 的 NuGet 套件支援 .NET Framework 4.5+ 與 .NET Core 1.0+ 版本,相容性非常高。這篇...
-
6
为 ASPNETCORE 7 项目添加 Serilog 2023-01-01 3本文将介绍如何为 ASP.NET Core 项目添加 Serilog。添加 Serilog首先,我们需要在项目中添加 Serilog 的 NuGet...
-
5
少年!看你骨骼惊奇,是万中无一的练武奇才,我这儿有本武林秘籍,见与你有缘就送你了!
-
6
【译】32位 .NET Framework 项目的 WinForm 设计器选择 ...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK