You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

115 lines
4.7 KiB
C#

2 months ago
using Serilog;
using System;
using System.IO;
#region << 版 本 注 释 >>
/*--------------------------------------------------------------------
* (c) 2024 WenJY
* CLR4.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
{
/// <summary>
/// Serilog日志类
/// </summary>
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 日志记录器
/// <summary>
/// Info日志
/// </summary>
/// <param name="msg"></param>
/// <param name="callerFilePath"></param>
/// <param name="callerLineNumber"></param>
public void Info(string msg, [System.Runtime.CompilerServices.CallerFilePath] string callerFilePath = "", [System.Runtime.CompilerServices.CallerLineNumber] int callerLineNumber = 0)
{
2 months ago
Console.WriteLine(msg);
2 months ago
var className = Path.GetFileNameWithoutExtension(callerFilePath);
Info_logger.ForContext("ClassName", className).ForContext("LineNumber", callerLineNumber).Information(msg);
}
/// <summary>
/// Plc日志
/// </summary>
/// <param name="msg"></param>
/// <param name="callerFilePath"></param>
/// <param name="callerLineNumber"></param>
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);
}
/// <summary>
/// Error日志
/// </summary>
/// <param name="msg"></param>
/// <param name="ex"></param>
/// <param name="callerFilePath"></param>
/// <param name="callerLineNumber"></param>
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}<br>", msg);
}
else if (!string.IsNullOrEmpty(msg) && ex != null)
{
string errorMsg = BeautyErrorMsg(ex);
logger.Error("【附加信息】 : {0}<br>{1}", msg, errorMsg);
}
else if (string.IsNullOrEmpty(msg) && ex != null)
{
string errorMsg = BeautyErrorMsg(ex);
logger.Error(errorMsg);
}
}
/// <summary>
/// Agv日志
/// </summary>
/// <param name="msg"></param>
/// <param name="callerFilePath"></param>
/// <param name="callerLineNumber"></param>
//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} <br>【异常信息】:{1} <br>【堆栈调用】:{2}", ex.GetType().Name, ex.Message, ex.StackTrace);
errorMsg = errorMsg.Replace("\r\n", "<br>");
errorMsg = errorMsg.Replace("位置", "<strong style=\"color:red\">位置</strong>");
return errorMsg;
}
}
}