using Serilog;
using System;
using System.IO;
#region << 版 本 注 释 >>
/*--------------------------------------------------------------------
* 版权所有 (c) 2024 WenJY 保留所有权利。
* CLR版本:4.0.30319.42000
* 机器名称:T14-GEN3-7895
* 命名空间:SlnMesnac.Serilog
* 唯一标识:fae9d68d-1992-4a03-b299-19edd9fc786d
*
* 创建者:WenJY
* 电子邮箱:
* 创建时间:2024-12-26 8:46:21
* 版本:V1.0.0
* 描述:
*
*--------------------------------------------------------------------
* 修改人:
* 时间:
* 修改说明:
*
* 版本:V1.0.0
*--------------------------------------------------------------------*/
#endregion << 版 本 注 释 >>
namespace SlnMesnac.Serilog
{
///
/// 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 Error_logger = Log.ForContext("Module", "Error");
//private readonly ILogger Agv_logger = Log.ForContext("Module", "Agv"); // 新增 Agv 日志记录器
///
/// Info日志
///
///
///
///
public void Info(string msg, [System.Runtime.CompilerServices.CallerFilePath] string callerFilePath = "", [System.Runtime.CompilerServices.CallerLineNumber] int callerLineNumber = 0)
{
Console.WriteLine(msg);
var className = Path.GetFileNameWithoutExtension(callerFilePath);
Info_logger.ForContext("ClassName", className).ForContext("LineNumber", callerLineNumber).Information(msg);
}
///
/// Plc日志
///
///
///
///
public void Plc(string msg, [System.Runtime.CompilerServices.CallerFilePath] string callerFilePath = "", [System.Runtime.CompilerServices.CallerLineNumber] int callerLineNumber = 0)
{
var className = Path.GetFileNameWithoutExtension(callerFilePath);
Plc_logger.ForContext("ClassName", className).ForContext("LineNumber", callerLineNumber).Information(msg);
}
///
/// Error日志
///
///
///
///
///
public void Error(string msg, Exception ex = null, [System.Runtime.CompilerServices.CallerFilePath] string callerFilePath = "", [System.Runtime.CompilerServices.CallerLineNumber] int callerLineNumber = 0)
{
var className = Path.GetFileNameWithoutExtension(callerFilePath);
var logger = Error_logger.ForContext("ClassName", className).ForContext("LineNumber", callerLineNumber);
if (!string.IsNullOrEmpty(msg) && ex == null)
{
logger.Error("【附加信息】 : {0}
", msg);
}
else if (!string.IsNullOrEmpty(msg) && ex != null)
{
string errorMsg = BeautyErrorMsg(ex);
logger.Error("【附加信息】 : {0}
{1}", msg, errorMsg);
}
else if (string.IsNullOrEmpty(msg) && ex != null)
{
string errorMsg = BeautyErrorMsg(ex);
logger.Error(errorMsg);
}
}
///
/// Agv日志
///
///
///
///
//public void Agv(string msg, [System.Runtime.CompilerServices.CallerFilePath] string callerFilePath = "", [System.Runtime.CompilerServices.CallerLineNumber] int callerLineNumber = 0)
//{
// var className = Path.GetFileNameWithoutExtension(callerFilePath);
// Agv_logger.ForContext("ClassName", className).ForContext("LineNumber", callerLineNumber).Write(SerilogExtensions.AgvLevel, msg);
//}
private string BeautyErrorMsg(Exception ex)
{
string errorMsg = string.Format("【异常类型】:{0}
【异常信息】:{1}
【堆栈调用】:{2}", ex.GetType().Name, ex.Message, ex.StackTrace);
errorMsg = errorMsg.Replace("\r\n", "
");
errorMsg = errorMsg.Replace("位置", "位置");
return errorMsg;
}
}
}