Files
2026-02-20 15:31:44 +08:00

139 lines
4.3 KiB
C#

using System.Diagnostics;
using System.IO;
using System.Text;
namespace ShrlAlgoToolkit.Core.Assists;
public static class LogAssist
{
//public static void WriteSeriLog(string path, string message)
//{
// Log.Logger = new LoggerConfiguration().WriteTo.File(path).CreateLogger();
// Log.Error(message);
// Log.CloseAndFlush();
//}
public static void ToLog(this string strLog, string logFolder = default)
{
if (logFolder == default)
{
var assemblyPath = typeof(LogAssist).Assembly.Location;
var directory = Path.GetDirectoryName(assemblyPath);
logFolder = Path.Combine(directory, "Logs");
}
if (!Directory.Exists(logFolder))
{
Directory.CreateDirectory(logFolder);
}
//logFolder = Directory.GetCurrentDirectory();返回是Revit目录
var logFile = Path.Combine(logFolder, DateTime.Now.ToString("yyyy-MM-dd") + ".log");
var fs = File.Exists(logFile)
? new FileStream(logFile, FileMode.Append, FileAccess.Write)
: new FileStream(logFile, FileMode.Create, FileAccess.Write);
using StreamWriter sw = new(fs);
sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss-ffff : ") + strLog);
sw.Close();
fs.Close();
}
public static void ToLog(this Exception execption, string logFolder = default)
{
if (logFolder == default)
{
var assemblyPath = typeof(LogAssist).Assembly.Location;
var directory = Path.GetDirectoryName(assemblyPath);
logFolder = $"{directory}\\Logs";
}
if (Directory.Exists(logFolder))
{
logFolder = Directory.GetCurrentDirectory();
}
else
{
Directory.CreateDirectory(logFolder);
}
var logFile = Path.Combine(logFolder, DateTime.Now.ToString("yyyy-MM-dd") + ".log");
var fs = File.Exists(logFile)
? new FileStream(logFile, FileMode.Append, FileAccess.Write)
: new FileStream(logFile, FileMode.Create, FileAccess.Write);
using StreamWriter sw = new(fs);
sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss-ffff : ") + execption.Message);
sw.Close();
fs.Close();
}
/// <summary>
/// 输出到桌面
/// </summary>
/// <param name="sb"></param>
/// <param name="fileName"></param>
public static void WriteTextFile(this StringBuilder sb, string fileName = "输出日志")
{
var filePath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + $"\\{fileName}.txt";
File.WriteAllText(filePath, sb.ToString());
System.Diagnostics.Process.Start(filePath);
}
public static void WriteTextFile(this string lineContent, string filePath)
{
FileStream fs = new(filePath, FileMode.Create, FileAccess.Write);
using StreamWriter sw = new(fs);
sw.WriteLine(lineContent);
sw.Close();
fs.Close();
}
public static string GetCurrentMethodFullName()
{
string str1;
try
{
var num = 2;
StackTrace stackTrace = new();
var length = stackTrace.GetFrames()!.Length;
StackFrame frame;
string str;
bool flag;
do
{
var num1 = num;
num = num1 + 1;
frame = stackTrace.GetFrame(num1);
str = frame.GetMethod().DeclaringType?.ToString();
flag = str!.EndsWith("Exception") && num < length;
} while (flag);
var name = frame.GetMethod().Name;
str1 = string.Concat(str, ".", name);
}
catch
{
str1 = null;
}
return str1;
}
public static string ToChineseMessage(this Exception ex)
{
while (ex.InnerException != null)
{
ex = ex.InnerException;
}
var t = ex.GetType();
var currentUICulture = System.Threading.Thread.CurrentThread.CurrentUICulture;
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("zh-CN");
var o = Activator.CreateInstance(t);
System.Threading.Thread.CurrentThread.CurrentUICulture = currentUICulture;
return ((Exception)o).Message;
}
}