添加项目文件。
This commit is contained in:
13
Logger/Logger.csproj
Normal file
13
Logger/Logger.csproj
Normal file
@@ -0,0 +1,13 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="NLog" Version="6.1.3" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
114
Logger/LoggerHelper.cs
Normal file
114
Logger/LoggerHelper.cs
Normal file
@@ -0,0 +1,114 @@
|
||||
using NLog;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Logger
|
||||
{
|
||||
public static class LoggerHelper
|
||||
{
|
||||
public static readonly ILogger Logger = LogManager.GetLogger("InfoLogger");
|
||||
public static readonly ILogger sqlLogger = LogManager.GetLogger("SqlLogger");
|
||||
public static IProgress<(string message, string color, int depth)> Progress { get; set; }
|
||||
static LoggerHelper()
|
||||
{
|
||||
Progress = new Progress<(string message, string color, int depth)>();
|
||||
}
|
||||
public static void InfoWithNotify(string message, int depth = 0)
|
||||
{
|
||||
Logger.Info(message); // 写入 NLog
|
||||
NotifyUI(message, "blue", depth); // 触发UI显示
|
||||
}
|
||||
|
||||
public static void SuccessWithNotify(string message, int depth = 0)
|
||||
{
|
||||
Logger.Info(message);
|
||||
NotifyUI(message, "lightgreen", depth);
|
||||
}
|
||||
public static void WarnWithNotify(string message, string stackTrace = null, int depth = 0)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(stackTrace))
|
||||
{
|
||||
string location = GetProjectStackLine(stackTrace);
|
||||
message = $"{message} ({location})";
|
||||
}
|
||||
|
||||
Logger.Warn(message);
|
||||
NotifyUI(message, "orange", depth);
|
||||
}
|
||||
|
||||
public static void ErrorWithNotify(string message, string stackTrace = null, int depth = 0)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(stackTrace))
|
||||
{
|
||||
string location = GetProjectStackLine(stackTrace);
|
||||
message = $"{message} ({location})";
|
||||
}
|
||||
|
||||
Logger.Error(message);
|
||||
NotifyUI(message, "red", depth);
|
||||
}
|
||||
private static void NotifyUI(string message, string color, int depth)
|
||||
{
|
||||
Progress.Report((message, color, depth));
|
||||
}
|
||||
|
||||
public static void Info(string message, int depth = 0)
|
||||
{
|
||||
Logger.Info(message);
|
||||
}
|
||||
public static void Success(string message, int depth = 0)
|
||||
{
|
||||
Logger.Info(message);
|
||||
}
|
||||
public static void Warn(string message, string stackTrace = null, int depth = 0)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(stackTrace))
|
||||
{
|
||||
string location = GetProjectStackLine(stackTrace);
|
||||
message = $"{message} ({location})";
|
||||
}
|
||||
|
||||
Logger.Warn(message);
|
||||
}
|
||||
|
||||
public static void Error(string message, string stackTrace = null, int depth = 0)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(stackTrace))
|
||||
{
|
||||
string location = GetProjectStackLine(stackTrace);
|
||||
message = $"{message} ({location})";
|
||||
}
|
||||
|
||||
Logger.Error(message);
|
||||
}
|
||||
|
||||
public static string GetProjectStackLine(string stackTrace)
|
||||
{
|
||||
if (string.IsNullOrEmpty(stackTrace))
|
||||
return "未知位置";
|
||||
|
||||
var lines = stackTrace.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
|
||||
|
||||
foreach (var line in lines)
|
||||
{
|
||||
// 匹配你项目的命名空间路径
|
||||
if (line.Contains("ADP"))
|
||||
{
|
||||
// 提取 "in 文件路径:line 行号"
|
||||
var match = Regex.Match(line, @"in (.+?):line (\d+)");
|
||||
if (match.Success)
|
||||
{
|
||||
return match.Value; // 返回类似 C:\...\MainViewModel.cs:line 37
|
||||
}
|
||||
return line.Trim();
|
||||
}
|
||||
}
|
||||
|
||||
return lines[0].Trim(); // 如果找不到就返回第一条
|
||||
}
|
||||
}
|
||||
}
|
||||
37
Logger/Nlog.config
Normal file
37
Logger/Nlog.config
Normal file
@@ -0,0 +1,37 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
||||
|
||||
<!-- 定义目标 -->
|
||||
<targets>
|
||||
<!-- SQL 日志 -->
|
||||
<target name="sqlLog" xsi:type="File"
|
||||
fileName="Logs/${shortdate}_sql.txt"
|
||||
layout="${longdate} [${level}] [ThreadId:${threadid}] ${message} ${exception}"
|
||||
encoding="utf-8" />
|
||||
|
||||
<!-- Info 日志 -->
|
||||
<target name="infoLog" xsi:type="File"
|
||||
fileName="Logs/${shortdate}_info.txt"
|
||||
layout="${longdate} [${level}] [ThreadId:${threadid}] ${message} ${exception}"
|
||||
encoding="utf-8" />
|
||||
|
||||
<!-- Warn 日志 -->
|
||||
<target name="warnLog" xsi:type="File"
|
||||
fileName="Logs/${shortdate}_warn.txt"
|
||||
layout="${longdate} [${level}] [ThreadId:${threadid}] ${message} ${exception}"
|
||||
encoding="utf-8" />
|
||||
</targets>
|
||||
|
||||
<!-- 定义日志规则 -->
|
||||
<rules>
|
||||
<!-- SQL 日志规则 -->
|
||||
<logger name="SqlLogger" minlevel="Info" writeTo="sqlLog" />
|
||||
|
||||
<!-- Info 日志规则 -->
|
||||
<logger name="InfoLogger" minlevel="Info" maxlevel="Info" writeTo="infoLog" />
|
||||
|
||||
<!-- Warn 日志规则 -->
|
||||
<logger name="*" minlevel="Warn" writeTo="warnLog" />
|
||||
</rules>
|
||||
</nlog>
|
||||
Reference in New Issue
Block a user