add - 添加Serilog日志

master
wenjy 4 weeks ago
parent 71f4127c39
commit 54ac8d2b5a

@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

@ -0,0 +1,69 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Serilog;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
#region << 版 本 注 释 >>
/*--------------------------------------------------------------------
* (c) 2026 WenJY
* CLR4.0.30319.42000
* T14-GEN3-7895
* Sln.Wcs.Serilog
* 8c408f49-7da2-4484-905a-c9ca16565026
*
* WenJY
*
* 2026-03-19 10:35:27
* V1.0.0
*
*
*--------------------------------------------------------------------
*
*
*
*
* V1.0.0
*--------------------------------------------------------------------*/
#endregion << 版 本 注 释 >>
namespace Sln.Wcs.Serilog
{
public static class SerilogExtensions
{
public static void UseSerilogExtensions(this IServiceProvider service)
{
//启用Serilog中间件
#region 通过配置文件读取日志存放位置
var configuration = service.GetService<IConfiguration>();
var logPath = Path.Combine(configuration["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("Agv"))
.WriteTo.File(Path.Combine($"{logPath}/Agv/", "Agv.log"), rollingInterval: RollingInterval.Day))
.WriteTo.Logger(lc => lc
.Filter.ByIncludingOnly(logEvent => logEvent.Properties.ContainsKey("Module") && logEvent.Properties["Module"].ToString().Contains("Rfid"))
.WriteTo.File(Path.Combine($"{logPath}/Rfid/", "Rfid.log"), rollingInterval: RollingInterval.Day))
.WriteTo.Logger(lc => lc
.Filter.ByIncludingOnly(logEvent => logEvent.Properties.ContainsKey("Module") && logEvent.Properties["Module"].ToString().Contains("Alarm"))
.WriteTo.File(Path.Combine($"{logPath}/Alarm/", "Alarm.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();
}
}
}

@ -0,0 +1,132 @@
using Serilog;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
#region << 版 本 注 释 >>
/*--------------------------------------------------------------------
* (c) 2026 WenJY
* CLR4.0.30319.42000
* T14-GEN3-7895
* Sln.Wcs.Serilog
* 53133fd1-c671-497c-9484-f1d8794cf283
*
* WenJY
*
* 2026-03-19 10:43:05
* V1.0.0
*
*
*--------------------------------------------------------------------
*
*
*
*
* V1.0.0
*--------------------------------------------------------------------*/
#endregion << 版 本 注 释 >>
namespace Sln.Wcs.Serilog
{
public class SerilogHelper
{
private readonly ILogger? Info_logger = Log.ForContext("Module", "Info");
private readonly ILogger? Plc_logger = Log.ForContext("Module", "Plc");
private readonly ILogger? Agv_logger = Log.ForContext("Module", "Agv");
private readonly ILogger? Rfid_logger = Log.ForContext("Module", "Rfid");
private readonly ILogger? Error_logger = Log.ForContext("Module", "Error");
private readonly ILogger? Alarm_logger = Log.ForContext("Module", "Alarm");
/// <summary>
/// Info日志
/// </summary>
/// <param name="msg"></param>
public void Info(string msg)
{
if (Info_logger != null)
{
this.Info_logger.Information(msg);
}
}
/// <summary>
/// Plc日志
/// </summary>
/// <param name="msg"></param>
public void Plc(string msg)
{
if (Plc_logger != null)
{
this.Plc_logger.Information(msg);
}
}
/// <summary>
/// Agv日志
/// </summary>
/// <param name="msg"></param>
public void Agv(string msg)
{
if (Agv_logger != null)
{
this.Agv_logger.Information(msg);
}
}
/// <summary>
/// Rfid日志
/// </summary>
/// <param name="msg"></param>
public void Rfid(string msg)
{
if (Rfid_logger != null)
{
this.Rfid_logger.Information(msg);
}
}
/// <summary>
/// 设备告警日志
/// </summary>
/// <param name="msg"></param>
public void Alarm(string msg)
{
if (Alarm_logger != null)
{
this.Alarm_logger.Information(msg);
}
}
/// <summary>
/// Error日志
/// </summary>
/// <param name="msg"></param>
/// <param name="ex"></param>
public void Error(string msg, Exception ex = null)
{
if (!string.IsNullOrEmpty(msg) && ex == null)
{
this.Error_logger.Information("【附加信息】 : {0}<br>", new object[] { msg });
}
else if (!string.IsNullOrEmpty(msg) && ex != null)
{
string errorMsg = BeautyErrorMsg(ex);
this.Error_logger.Information("【附加信息】 : {0}<br>{1}", new object[] { msg, errorMsg });
}
else if (string.IsNullOrEmpty(msg) && ex != null)
{
string errorMsg = BeautyErrorMsg(ex);
this.Error_logger.Information(errorMsg);
}
}
private string BeautyErrorMsg(Exception ex)
{
string errorMsg = string.Format("【异常类型】:{0} <br>【异常信息】:{1} <br>【堆栈调用】:{2}", new object[] { ex.GetType().Name, ex.Message, ex.StackTrace });
errorMsg = errorMsg.Replace("\r\n", "<br>");
errorMsg = errorMsg.Replace("位置", "<strong style=\"color:red\">位置</strong>");
return errorMsg;
}
}
}

@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Serilog.AspNetCore" Version="10.0.0" />
</ItemGroup>
</Project>

@ -10,6 +10,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sln.Wcs.HikRoBotSdk", "Sln.
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sln.Wcs.HikRoBotApi", "Sln.Wcs.HikRoBotApi\Sln.Wcs.HikRoBotApi.csproj", "{9E3193CA-590C-4965-B2EF-02C2AE252095}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sln.Wcs.Serilog", "Sln.Wcs.Serilog\Sln.Wcs.Serilog.csproj", "{DD340736-51E7-47D5-AF1E-24A79F8B4675}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -32,6 +34,10 @@ Global
{9E3193CA-590C-4965-B2EF-02C2AE252095}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9E3193CA-590C-4965-B2EF-02C2AE252095}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9E3193CA-590C-4965-B2EF-02C2AE252095}.Release|Any CPU.Build.0 = Release|Any CPU
{DD340736-51E7-47D5-AF1E-24A79F8B4675}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DD340736-51E7-47D5-AF1E-24A79F8B4675}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DD340736-51E7-47D5-AF1E-24A79F8B4675}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DD340736-51E7-47D5-AF1E-24A79F8B4675}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

@ -3,6 +3,7 @@ using Com.Ctrip.Framework.Apollo;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Sln.Wcs.HikRoBotSdk;
using Sln.Wcs.Serilog;
namespace Sln.Wcs
{
@ -17,11 +18,10 @@ namespace Sln.Wcs
var serviceProvider = services.BuildServiceProvider();
var sdk = serviceProvider.GetService<IHIKRoBotSdk>();
//var info = sdk.GenAgvSchedulingTask(null);
Console.WriteLine("\n程序执行完成");
serviceProvider.UseSerilogExtensions();
var config = serviceProvider.GetService<IConfiguration>();
var log = serviceProvider.GetService<SerilogHelper>();
log.Info($"系统启动成功,日志存放位置:{config["logPath"]}");
}
@ -64,6 +64,7 @@ namespace Sln.Wcs
.AsSelf()
.WithTransientLifetime());
services.AddSingleton(typeof(SerilogHelper));
}
}

@ -22,13 +22,13 @@
<PackageReference Include="Com.Ctrip.Framework.Apollo" Version="2.11.0" />
<PackageReference Include="Com.Ctrip.Framework.Apollo.Configuration" Version="2.11.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
<PackageReference Include="Scrutor" Version="7.0.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Sln.Wcs.HikRoBotApi\Sln.Wcs.HikRoBotApi.csproj" />
<ProjectReference Include="..\Sln.Wcs.HikRoBotSdk\Sln.Wcs.HikRoBotSdk.csproj" />
<ProjectReference Include="..\Sln.Wcs.Serilog\Sln.Wcs.Serilog.csproj" />
</ItemGroup>
</Project>

@ -8,7 +8,7 @@
"**/platforms"
],
"apollo": {
"AppId": "SampleApp",
"AppId": "SlnWcs",
"Env": "DEV",
"MetaServer": "http://119.45.202.115:4320",
"ConfigServer": [

Loading…
Cancel
Save