You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
258 lines
9.4 KiB
258 lines
9.4 KiB
using Microsoft.Extensions.Logging; |
|
using Newtonsoft.Json; |
|
using System; |
|
using System.Collections.Generic; |
|
using System.IO; |
|
using System.Linq; |
|
using System.Text; |
|
using System.Threading.Tasks; |
|
using LogLevel = Elight.Utility.Enum.LogLevel; |
|
|
|
namespace Elight.Utility.logs |
|
{ |
|
/// <summary> |
|
/// 写入文本日志 |
|
/// </summary> |
|
public static class LogService |
|
{ |
|
|
|
/// <summary> |
|
/// 写日志的等级 默认int最小值 |
|
/// </summary> |
|
private static readonly int _logWriteLevel =3; |
|
|
|
/// <summary> |
|
/// The obj. |
|
/// </summary> |
|
private static readonly object obj = new object(); |
|
|
|
/// <summary> |
|
/// 写日志等级设置,默认只写全部日志 |
|
/// </summary> |
|
private static LogLevel LogWriteLevel |
|
{ |
|
get |
|
{ |
|
if (_logWriteLevel < 0 || _logWriteLevel > 7) |
|
{ |
|
return LogLevel.H调试信息; |
|
} |
|
else |
|
{ |
|
return (LogLevel)_logWriteLevel; |
|
} |
|
} |
|
} |
|
|
|
/// <summary> |
|
/// 记录异常文本日志 |
|
/// </summary> |
|
/// <param name="ex">异常</param> |
|
/// <param name="remark">备注</param> |
|
public static void WriteLog(Exception ex, string remark) |
|
{ |
|
WriteLog(ex, null, remark); |
|
} |
|
|
|
/// <summary> |
|
/// Writes the log. |
|
/// </summary> |
|
/// <param name="ex">The ex.</param> |
|
/// <param name="remark">The remark.</param> |
|
/// <param name="logLevel">The log level.</param> |
|
public static void WriteLog(Exception ex, string remark, LogLevel logLevel = LogLevel.D错误事件) |
|
{ |
|
if (logLevel > LogWriteLevel) |
|
{ |
|
return; |
|
} |
|
else |
|
{ |
|
WriteLog(ex, null, remark); |
|
} |
|
} |
|
|
|
/// <summary> |
|
/// 记录异常文本日志 |
|
/// </summary> |
|
/// <param name="ex">异常</param> |
|
/// <param name="path">日志路径</param> |
|
/// <param name="remark">备注</param> |
|
public static void WriteLog(Exception ex, string path, string remark) |
|
{ |
|
var errormessage = CreateErrorMessage(ex, remark); |
|
WriteLog(errormessage.ToString(), path ?? Path.Combine(GetLogPath(), "Logs/ExceptionLog")); |
|
} |
|
|
|
/// <summary> |
|
/// 记录异常文本日志 |
|
/// </summary> |
|
/// <param name="describe">错误描述</param> |
|
/// <param name="ex">异常</param> |
|
/// <param name="path">日志路径</param> |
|
/// <param name="remark">备注</param> |
|
public static void WriteLog(string describe, Exception ex, string path, string remark) |
|
{ |
|
var errormessage = CreateErrorMessage(ex, remark); |
|
WriteLog("Describe:" + describe + " Error:" + errormessage, path ?? Path.Combine(GetLogPath(), "Logs/ExceptionLog")); |
|
} |
|
|
|
/// <summary> |
|
/// 创建异常消息 |
|
/// </summary> |
|
/// <param name="ex">异常信息</param> |
|
/// <param name="remark">备注</param> |
|
/// <returns>结果</returns> |
|
private static StringBuilder CreateErrorMessage(Exception ex, string remark) |
|
{ |
|
StringBuilder stringBuilder = new StringBuilder(); |
|
stringBuilder.Append("************************Exception Start********************************"); |
|
string newLine = Environment.NewLine; |
|
stringBuilder.Append(newLine); |
|
stringBuilder.AppendLine("Exception Remark:" + remark); |
|
Exception innerException = ex.InnerException; |
|
stringBuilder.AppendFormat("Exception Date:{0}{1}", DateTime.Now, Environment.NewLine); |
|
if (innerException != null) |
|
{ |
|
stringBuilder.AppendFormat("Inner Exception Type:{0}{1}", innerException.GetType(), newLine); |
|
stringBuilder.AppendFormat("Inner Exception Message:{0}{1}", innerException.Message, newLine); |
|
stringBuilder.AppendFormat("Inner Exception Source:{0}{1}", innerException.Source, newLine); |
|
stringBuilder.AppendFormat("Inner Exception StackTrace:{0}{1}", innerException.StackTrace, newLine); |
|
} |
|
stringBuilder.AppendFormat("Exception Type:{0}{1}", ex.GetType(), newLine); |
|
stringBuilder.AppendFormat("Exception Message:{0}{1}", ex.Message, newLine); |
|
stringBuilder.AppendFormat("Exception Source:{0}{1}", ex.Source, newLine); |
|
stringBuilder.AppendFormat("Exception StackTrace:{0}{1}", ex.StackTrace, newLine); |
|
stringBuilder.Append("************************Exception End************************************"); |
|
stringBuilder.Append(newLine); |
|
return stringBuilder; |
|
} |
|
|
|
/// <summary> |
|
/// 记录文本日志 |
|
/// </summary> |
|
/// <param name="content">日志内容</param> |
|
/// <param name="logLevel">The log level.</param> |
|
public static void WriteLog(string content, LogLevel logLevel = LogLevel.G有用信息) |
|
{ |
|
if (logLevel > LogWriteLevel) |
|
{ |
|
return; |
|
} |
|
else |
|
{ |
|
WriteLog(logLevel.ToString() + "\t" + content, Path.Combine(GetLogPath(), "Logs")); |
|
} |
|
} |
|
|
|
/// <summary> |
|
/// 记录文本日志 |
|
/// </summary> |
|
/// <param name="content">日志内容</param> |
|
/// <param name="path">日志路径</param> |
|
public static void WriteLog(string content, string path) |
|
{ |
|
//Action action = () => Log(content, path); |
|
//action.BeginInvoke(null, null); |
|
//BeginInvoke只能在framework里使用 |
|
|
|
Task task = Task.Run(() => Log(content, path)); |
|
task.Wait(); |
|
} |
|
|
|
/// <summary> |
|
/// 记录文本日志 大于等于配置的等级 才写日志 |
|
/// </summary> |
|
/// <param name="content">日志内容</param> |
|
/// <param name="path">日志路径</param> |
|
/// <param name="logLevel">写日志的等级</param> |
|
public static void WriteLog(string content, string path, LogLevel logLevel = LogLevel.G有用信息) |
|
{ |
|
if (logLevel > LogWriteLevel) |
|
{ |
|
return; |
|
} |
|
WriteLog(logLevel + "\t" + content, path); |
|
} |
|
|
|
/// <summary> |
|
/// 记录日志 |
|
/// </summary> |
|
/// <param name="method">调用方法(必填)</param> |
|
/// <param name="request">请求参数</param> |
|
/// <param name="response">输出参数</param> |
|
/// <param name="saveFolder">保存文件夹,默认为CallLog</param> |
|
public static void SaveLog(string method, object request, object response, string saveFolder = "CallLog") |
|
{ |
|
//try |
|
//{ |
|
StringBuilder stringBuilder = new StringBuilder(); |
|
stringBuilder.Append("************************Start********************************"); |
|
stringBuilder.Append(Environment.NewLine); |
|
stringBuilder.AppendFormat("Method:{0}{1}", method, Environment.NewLine); |
|
stringBuilder.AppendFormat("Request:{0}{1}", request == null ? string.Empty : JsonConvert.SerializeObject(request), Environment.NewLine); |
|
stringBuilder.AppendFormat("Response:{0}{1}", response == null ? "void" : JsonConvert.SerializeObject(response), Environment.NewLine); |
|
stringBuilder.Append("************************End************************************"); |
|
stringBuilder.Append(Environment.NewLine); |
|
var logContent = stringBuilder.ToString(); |
|
var path = Path.Combine(GetLogPath(), "Logs/" + saveFolder); |
|
WriteLog(logContent, path); |
|
//} |
|
//catch (Exception ex) |
|
//{ |
|
// WriteLog(ex, "记录调用日志异常"); |
|
//} |
|
} |
|
|
|
/// <summary> |
|
/// The log. |
|
/// </summary> |
|
/// <param name="content"> |
|
/// The content. |
|
/// </param> |
|
/// <param name="path"> |
|
/// The path. |
|
/// </param> |
|
/// <returns> |
|
/// The |
|
/// </returns> |
|
internal static bool Log(string content, string path) |
|
{ |
|
lock (obj) |
|
{ |
|
try |
|
{ |
|
TextWriter textWriter = new TextWriter(path); |
|
return |
|
textWriter.WriteLog( |
|
DateTime.Now.ToString("日志时间:yyyy-MM-dd HH:mm:ss") + Environment.NewLine + content |
|
+ Environment.NewLine); |
|
} |
|
catch (Exception) |
|
{ |
|
return false; |
|
} |
|
} |
|
} |
|
|
|
/// <summary> |
|
/// 获取日志路径 |
|
/// </summary> |
|
/// <returns>路径</returns> |
|
internal static string GetLogPath() |
|
{ |
|
var _path = "/CaseFile/logs"; |
|
var dic = Path.Combine(Environment.CurrentDirectory, "wwwroot"); |
|
if (!string.IsNullOrEmpty(_path)) |
|
dic += _path; |
|
if (string.IsNullOrWhiteSpace(dic)) |
|
{ |
|
return AppDomain.CurrentDomain.BaseDirectory; |
|
} |
|
else |
|
{ |
|
return dic; |
|
} |
|
} |
|
} |
|
}
|
|
|