|
|
#region << 版 本 注 释 >>
|
|
|
|
|
|
/*--------------------------------------------------------------------
|
|
|
* 版权所有 (c) 2025 WenJY 保留所有权利。
|
|
|
* CLR版本:4.0.30319.42000
|
|
|
* 机器名称:Mr.Wen's MacBook Pro
|
|
|
* 命名空间:Sln.Iot.Serilog
|
|
|
* 唯一标识:77C04261-6831-4763-A6E1-C6B2B66D4DD9
|
|
|
*
|
|
|
* 创建者:WenJY
|
|
|
* 电子邮箱:
|
|
|
* 创建时间:2025-04-11 11:12:30
|
|
|
* 版本:V1.0.0
|
|
|
* 描述:
|
|
|
*
|
|
|
*--------------------------------------------------------------------
|
|
|
* 修改人:
|
|
|
* 时间:
|
|
|
* 修改说明:
|
|
|
*
|
|
|
* 版本:V1.0.0
|
|
|
*--------------------------------------------------------------------*/
|
|
|
|
|
|
#endregion << 版 本 注 释 >>
|
|
|
|
|
|
using System;
|
|
|
using System.IO;
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
using Serilog;
|
|
|
using Sln.Iot.Config;
|
|
|
|
|
|
namespace Sln.Iot.Serilog
|
|
|
{
|
|
|
public static class SerilogExtensions
|
|
|
{
|
|
|
public static void UseSerilogExtensions(this IServiceProvider service)
|
|
|
{
|
|
|
//启用Serilog中间件
|
|
|
|
|
|
|
|
|
#region 通过配置文件读取日志存放位置
|
|
|
var appConfig = service.GetService<AppConfig>();
|
|
|
var logPath = Path.Combine(appConfig.logPath, "Logs");
|
|
|
#endregion
|
|
|
|
|
|
Log.Logger = new LoggerConfiguration().MinimumLevel.Information().WriteTo.Console()
|
|
|
.WriteTo.Logger(lc => lc
|
|
|
.Filter.ByIncludingOnly(logEvent => logEvent.Properties.ContainsKey("Module") && logEvent.Properties["Module"].ToString().Contains("Info"))
|
|
|
.WriteTo.File(Path.Combine($"{logPath}/Info/", "Info.log"), rollingInterval: RollingInterval.Day))
|
|
|
.WriteTo.Logger(lc => lc
|
|
|
.Filter.ByIncludingOnly(logEvent => logEvent.Properties.ContainsKey("Module") && logEvent.Properties["Module"].ToString().Contains("Plc"))
|
|
|
.WriteTo.File(Path.Combine($"{logPath}/Plc/", "Plc.log"), rollingInterval: RollingInterval.Day))
|
|
|
.WriteTo.Logger(lc => lc
|
|
|
.Filter.ByIncludingOnly(logEvent => logEvent.Properties.ContainsKey("Module") && logEvent.Properties["Module"].ToString().Contains("Camera"))
|
|
|
.WriteTo.File(Path.Combine($"{logPath}/Camera/", "Camera.log"), rollingInterval: RollingInterval.Day))
|
|
|
.WriteTo.Logger(lc => lc
|
|
|
.Filter.ByIncludingOnly(logEvent => logEvent.Properties.ContainsKey("Module") && logEvent.Properties["Module"].ToString().Contains("Error"))
|
|
|
.WriteTo.File(Path.Combine($"{logPath}/Error/", "Error.log"), rollingInterval: RollingInterval.Day))
|
|
|
.CreateLogger();
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
}
|
|
|
} |