123 lines
4.2 KiB
C#
123 lines
4.2 KiB
C#
using System;
|
|
using System.Diagnostics;
|
|
using System.IO;
|
|
using System.Text;
|
|
|
|
namespace Szmedi.Toolkit.Assists
|
|
{
|
|
public static class LogAssists
|
|
{
|
|
|
|
private 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 void ToLog(this string strLog, string logFolder = default)
|
|
{
|
|
if (logFolder == default)
|
|
{
|
|
var assemblyPath = typeof(LogAssists).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 WriteLog(string strLog, string logFolder = default)
|
|
{
|
|
try
|
|
{
|
|
if (logFolder == default)
|
|
{
|
|
string assemblyPath = typeof(LogAssists).Assembly.Location;
|
|
string directory = Path.GetDirectoryName(assemblyPath);
|
|
logFolder = $"{directory}\\Logs";
|
|
}
|
|
|
|
if (Directory.Exists(logFolder))
|
|
{
|
|
logFolder = Directory.GetCurrentDirectory();
|
|
}
|
|
else
|
|
{
|
|
Directory.CreateDirectory(logFolder);
|
|
}
|
|
string logFile = Path.Combine(logFolder, DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss: ") + ".log");
|
|
|
|
FileStream 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: ") + strLog);
|
|
sw.Close();
|
|
fs.Close();
|
|
}
|
|
catch
|
|
{
|
|
// ignored
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 输出到桌面
|
|
/// </summary>
|
|
/// <param name="fileName"></param>
|
|
/// <param name="message"></param>
|
|
public static void WriteTxtFile(string fileName, string message)
|
|
{
|
|
string filePath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) +
|
|
$"\\{fileName}.txt";
|
|
File.WriteAllText(filePath, message, Encoding.UTF8);
|
|
System.Diagnostics.Process.Start(filePath);
|
|
}
|
|
public static void WriteCSVFile(string fileName, string message)
|
|
{
|
|
string filePath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) +
|
|
$"\\{fileName}.csv";
|
|
File.WriteAllText(filePath, message, Encoding.UTF8);
|
|
//System.Diagnostics.Process.Start(filePath);
|
|
}
|
|
}
|
|
}
|