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
{
///
/// 写入文本日志
///
public static class LogService
{
///
/// 写日志的等级 默认int最小值
///
private static readonly int _logWriteLevel =3;
///
/// The obj.
///
private static readonly object obj = new object();
///
/// 写日志等级设置,默认只写全部日志
///
private static LogLevel LogWriteLevel
{
get
{
if (_logWriteLevel < 0 || _logWriteLevel > 7)
{
return LogLevel.H调试信息;
}
else
{
return (LogLevel)_logWriteLevel;
}
}
}
///
/// 记录异常文本日志
///
/// 异常
/// 备注
public static void WriteLog(Exception ex, string remark)
{
WriteLog(ex, null, remark);
}
///
/// Writes the log.
///
/// The ex.
/// The remark.
/// The log level.
public static void WriteLog(Exception ex, string remark, LogLevel logLevel = LogLevel.D错误事件)
{
if (logLevel > LogWriteLevel)
{
return;
}
else
{
WriteLog(ex, null, remark);
}
}
///
/// 记录异常文本日志
///
/// 异常
/// 日志路径
/// 备注
public static void WriteLog(Exception ex, string path, string remark)
{
var errormessage = CreateErrorMessage(ex, remark);
WriteLog(errormessage.ToString(), path ?? Path.Combine(GetLogPath(), "Logs/ExceptionLog"));
}
///
/// 记录异常文本日志
///
/// 错误描述
/// 异常
/// 日志路径
/// 备注
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"));
}
///
/// 创建异常消息
///
/// 异常信息
/// 备注
/// 结果
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;
}
///
/// 记录文本日志
///
/// 日志内容
/// The log level.
public static void WriteLog(string content, LogLevel logLevel = LogLevel.G有用信息)
{
if (logLevel > LogWriteLevel)
{
return;
}
else
{
WriteLog(logLevel.ToString() + "\t" + content, Path.Combine(GetLogPath(), "Logs"));
}
}
///
/// 记录文本日志
///
/// 日志内容
/// 日志路径
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();
}
///
/// 记录文本日志 大于等于配置的等级 才写日志
///
/// 日志内容
/// 日志路径
/// 写日志的等级
public static void WriteLog(string content, string path, LogLevel logLevel = LogLevel.G有用信息)
{
if (logLevel > LogWriteLevel)
{
return;
}
WriteLog(logLevel + "\t" + content, path);
}
///
/// 记录日志
///
/// 调用方法(必填)
/// 请求参数
/// 输出参数
/// 保存文件夹,默认为CallLog
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, "记录调用日志异常");
//}
}
///
/// The log.
///
///
/// The content.
///
///
/// The path.
///
///
/// The
///
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;
}
}
}
///
/// 获取日志路径
///
/// 路径
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;
}
}
}
}