XiheNet.Log 1.0.0

dotnet add package XiheNet.Log --version 1.0.0
                    
NuGet\Install-Package XiheNet.Log -Version 1.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="XiheNet.Log" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="XiheNet.Log" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="XiheNet.Log" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add XiheNet.Log --version 1.0.0
                    
#r "nuget: XiheNet.Log, 1.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package XiheNet.Log@1.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=XiheNet.Log&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=XiheNet.Log&version=1.0.0
                    
Install as a Cake Tool

简介

XiheNet.Log是对我们日常工作中使用Serilogd的整理,没有做任何功能添加,只是简单的打包,方便我们在项目中使用。

依赖项

  • Serilog
  • Serilog.Sinks.Console
  • Serilog.Sinks.File
  • Serilog.Sinks.Async
  • Serilog.AspNetCore
  • Serilog.Extensions.Logging

使用方法

  • 引入XiheNet.Log包
  • 修改appsettings.json,配置Serilog记录级别,方便发布后更改最小日志级别
"Serilog": {
        "MinimumLevel": {
            "Default": "Debug",         //最小日志记录级别
            "Override": {               //系统日志最小记录级别
                "System": "Warning",
                "Microsoft": "Warning"
            }
        }
    }
  • 修改Program.cs,增加日志功能的使用
using Serilog;
using Serilog.Events;

var builder = WebApplication.CreateBuilder(args);

// ………… 其他内容

// 使用日志
builder.Host.UseSerilog((context, logger) => {
	// 读取配置文件
    logger.ReadFrom.Configuration(context.Configuration);
    logger.Enrich.FromLogContext();

	// 输出到Console控制台
	logger.WriteTo.Console();

	// 定义日志输出模板
	string SerilogOutputTemplate = "时间:{Timestamp:yyyy-MM-dd HH:mm:ss.fff}{NewLine}等级:{Level}{NewLine}来源:{SourceContext}{NewLine}日志:{Message}{NewLine}{Exception}{NewLine}";

	// 输出到配置的文件日志目录
	string serilogDebug = System.Environment.CurrentDirectory + "\\Logs\\2 - Debug\\.log";
	logger.WriteTo.Logger(lg => lg.Filter.ByIncludingOnly(p => p.Level == LogEventLevel.Debug).WriteTo.Async(a => a.File(serilogDebug, rollingInterval: RollingInterval.Day, outputTemplate: SerilogOutputTemplate)));
	string serilogInfo = System.Environment.CurrentDirectory + "\\Logs\\3 - Info\\.log";
	logger.WriteTo.Logger(lg => lg.Filter.ByIncludingOnly(p => p.Level == LogEventLevel.Information).WriteTo.Async(a => a.File(serilogInfo, rollingInterval: RollingInterval.Day, outputTemplate: SerilogOutputTemplate)));
	string serilogWarn = System.Environment.CurrentDirectory + "\\Logs\\4 - Warning\\.log";
	logger.WriteTo.Logger(lg => lg.Filter.ByIncludingOnly(p => p.Level == LogEventLevel.Warning).WriteTo.Async(a => a.File(serilogWarn, rollingInterval: RollingInterval.Day, outputTemplate: SerilogOutputTemplate)));
	string serilogError = System.Environment.CurrentDirectory + "\\Logs\\5 - Error\\.log";
	logger.WriteTo.Logger(lg => lg.Filter.ByIncludingOnly(p => p.Level == LogEventLevel.Error).WriteTo.Async(a => a.File(serilogError, rollingInterval: RollingInterval.Day, outputTemplate: SerilogOutputTemplate)));
	string serilogFatal = System.Environment.CurrentDirectory + "\\Logs\\6 - Fatal\\.log";
	logger.WriteTo.Logger(lg => lg.Filter.ByIncludingOnly(p => p.Level == LogEventLevel.Fatal).WriteTo.Async(a => a.File(serilogFatal, rollingInterval: RollingInterval.Day, outputTemplate: SerilogOutputTemplate)));
});

var app = builder.Build();

// ………… 其他内容
  • 使用前注入
private readonly ILogger<IndexModel> _logger;
public IndexModel(
	ILogger<IndexModel> logger
) {
	_logger = logger;
}
  • 记录日志
_logger.LogDebug("LogDebug");
_logger.LogInformation("LogInformation");
_logger.LogWarning("LogWarning");
_logger.LogError("LogError");
Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.0 366 9/14/2023