146 lines
5.0 KiB
C#
146 lines
5.0 KiB
C#
![]() |
using System;
|
|||
|
using System.IO;
|
|||
|
using System.Reflection;
|
|||
|
using Dl_Electrical.Helper;
|
|||
|
using Newtonsoft.Json.Linq;
|
|||
|
|
|||
|
namespace DI_Electrical.Helper
|
|||
|
{
|
|||
|
/// <summary>
|
|||
|
/// nLog使用帮助类
|
|||
|
/// </summary>
|
|||
|
public class LoggerHelper
|
|||
|
{
|
|||
|
private static readonly log4net.ILog loginfo = log4net.LogManager.GetLogger("loginfo");
|
|||
|
private static readonly log4net.ILog logerror = log4net.LogManager.GetLogger("logerror");
|
|||
|
private static readonly log4net.ILog logdebug = log4net.LogManager.GetLogger("logdebug");
|
|||
|
private static readonly log4net.ILog logwarn = log4net.LogManager.GetLogger("logwarn");
|
|||
|
private static readonly log4net.ILog logfatal = log4net.LogManager.GetLogger("logfatal");
|
|||
|
|
|||
|
private static LoggerHelper _obj = null;
|
|||
|
private static string logPath = string.Empty;
|
|||
|
private LoggerHelper()
|
|||
|
{
|
|||
|
string codeBase = Assembly.GetExecutingAssembly().CodeBase;
|
|||
|
string dllPath = codeBase.Replace("file:///", "");
|
|||
|
dllPath = Path.GetDirectoryName(dllPath);
|
|||
|
logPath = Path.Combine(dllPath, "Logs\\");
|
|||
|
var configFile = new FileInfo(Path.Combine(dllPath, "log4net.config"));
|
|||
|
log4net.Config.XmlConfigurator.ConfigureAndWatch(configFile);
|
|||
|
}
|
|||
|
/// <summary>
|
|||
|
/// 获取当前的日志记录<see cref="LoggerHelper"/>对象。
|
|||
|
/// </summary>
|
|||
|
public static LoggerHelper Current
|
|||
|
{
|
|||
|
get => _obj ?? (new LoggerHelper());
|
|||
|
set => _obj = value;
|
|||
|
}
|
|||
|
|
|||
|
#region Debug,调试
|
|||
|
/// <summary>
|
|||
|
/// 调试信息输出。
|
|||
|
/// </summary>
|
|||
|
/// <param name="msg">需要记录的信息。</param>
|
|||
|
public void Debug(string msg)
|
|||
|
{
|
|||
|
logdebug.Debug(msg);
|
|||
|
}
|
|||
|
|
|||
|
#endregion
|
|||
|
|
|||
|
#region Info,信息
|
|||
|
/// <summary>
|
|||
|
/// 普通信息输出。
|
|||
|
/// </summary>
|
|||
|
/// <param name="msg">需要记录的信息。</param>
|
|||
|
public void Info(string msg)
|
|||
|
{
|
|||
|
loginfo.Info(msg);
|
|||
|
}
|
|||
|
|
|||
|
#endregion
|
|||
|
|
|||
|
#region Warn,警告
|
|||
|
/// <summary>
|
|||
|
/// 警告级别信息输出。
|
|||
|
/// </summary>
|
|||
|
/// <param name="msg">需要记录的信息。</param>
|
|||
|
public void Warn(string msg)
|
|||
|
{
|
|||
|
logwarn.Warn(msg);
|
|||
|
}
|
|||
|
|
|||
|
#endregion
|
|||
|
|
|||
|
#region Error,错误
|
|||
|
/// <summary>
|
|||
|
/// 错误级别信息输出。
|
|||
|
/// </summary>
|
|||
|
/// <param name="msg">需要记录的信息。</param>
|
|||
|
public void Error(string msg)
|
|||
|
{
|
|||
|
logerror.Error("----------------------------Error BEGIN------------------------------");
|
|||
|
logerror.Error(msg);
|
|||
|
logerror.Error("-----------------------------Error END-------------------------------");
|
|||
|
}
|
|||
|
#endregion
|
|||
|
|
|||
|
#region Fatal,致命错误
|
|||
|
/// <summary>
|
|||
|
/// 致命错误级别信息输出。
|
|||
|
/// </summary>
|
|||
|
/// <param name="msg">需要记录的信息。</param>
|
|||
|
/// <param name="err">需要记录的系统异常。</param>
|
|||
|
public void Fatal(string msg)
|
|||
|
{
|
|||
|
logfatal.Fatal("----------------------------Fatal BEGIN------------------------------");
|
|||
|
logerror.Fatal(msg);
|
|||
|
logerror.Fatal("-----------------------------Fatal END-------------------------------");
|
|||
|
}
|
|||
|
#endregion
|
|||
|
|
|||
|
#region 输出json日志
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 输出json日志
|
|||
|
/// </summary>
|
|||
|
/// <param name="funName">json方法名</param>
|
|||
|
/// <param name="json">json数据</param>
|
|||
|
public void WriteJson(string funName, string json)
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
//json路径文件名 Environment.ExpandEnvironmentVariables(@"%USERPROFILE%\AppData\Roaming\SWS\Logs\"
|
|||
|
string filename = Path.Combine(logPath, funName + ".json");
|
|||
|
//判断文件是否被打开占用
|
|||
|
if (!FileHelper.IsFileLocked(filename))
|
|||
|
{
|
|||
|
File.Delete(filename);
|
|||
|
string strJson = string.Empty;
|
|||
|
if (json.StartsWith("["))
|
|||
|
{
|
|||
|
//格式化json数据 当前为组类型
|
|||
|
JArray jobj = JArray.Parse(json);
|
|||
|
strJson = jobj.ToString();
|
|||
|
}
|
|||
|
else if (json.StartsWith("{"))
|
|||
|
{
|
|||
|
//格式化json数据 当前为类类型
|
|||
|
JObject jobj = JObject.Parse(json);
|
|||
|
strJson = jobj.ToString();
|
|||
|
}
|
|||
|
//创建json文件,并输出数据
|
|||
|
FileStream fs = new FileStream(filename, FileMode.Append);
|
|||
|
StreamWriter wr = null;
|
|||
|
wr = new StreamWriter(fs);
|
|||
|
wr.WriteLine(strJson);
|
|||
|
wr.Close();
|
|||
|
}
|
|||
|
}
|
|||
|
catch { }
|
|||
|
}
|
|||
|
#endregion
|
|||
|
}
|
|||
|
}
|