|
|
using Serilog;
|
|
|
using System;
|
|
|
using System.Collections.Generic;
|
|
|
using System.Text;
|
|
|
|
|
|
#region << 版 本 注 释 >>
|
|
|
/*--------------------------------------------------------------------
|
|
|
* 版权所有 (c) 2024 WenJY 保留所有权利。
|
|
|
* CLR版本:4.0.30319.42000
|
|
|
* 机器名称:T14-GEN3-7895
|
|
|
* 命名空间:SlnMesnac.Serilog
|
|
|
* 唯一标识:a537790e-bf7c-4dd6-ade7-fc8cd5bd6d0d
|
|
|
*
|
|
|
* 创建者:WenJY
|
|
|
* 电子邮箱:
|
|
|
* 创建时间:2024-10-12 10:42:44
|
|
|
* 版本: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? Camera_logger = Log.ForContext("Module", "Camera");
|
|
|
|
|
|
/// <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>
|
|
|
/// 相机日志
|
|
|
/// </summary>
|
|
|
/// <param name="msg"></param>
|
|
|
public void Camera(string msg)
|
|
|
{
|
|
|
if (Camera_logger != null)
|
|
|
{
|
|
|
this.Camera_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;
|
|
|
}
|
|
|
|
|
|
}
|
|
|
}
|