#region [自定义类-VS][20250701112200484][AutoThread] using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Collections.Concurrent; using System.ComponentModel; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; using GZ.Modular.Redis; using static System.Windows.Forms.VisualStyles.VisualStyleElement; using System.Windows.Interop; using static System.Windows.Forms.VisualStyles.VisualStyleElement.TextBox; using System.Security.Cryptography; using System.Windows.Markup; using static System.Runtime.CompilerServices.RuntimeHelpers; using ServiceStack.Configuration; using ServiceStack; using static Dapper.SqlMapper; using System.IO; using System.Net.WebSockets; using System.Net; using System.Threading; using System.Net.Sockets; using NLog.Config; using NLog.Targets; using NLog; using ServiceStack.Messaging.Rcon; namespace GZ.Projects.HnSx { public partial class AutoThread { private static AutoThread _instance; // 私有构造函数防止外部实例化 private AutoThread() { } public static AutoThread Instance { get { if (_instance == null) { _instance = new AutoThread(); } return _instance; } } // 线程安全的委托缓存 private static readonly ConcurrentDictionary _methodCache = new ConcurrentDictionary(); // 方法执行器 public static object InvokeMethod(object instance, string methodName, params object[] args) { var cacheKey = $"{instance.GetType().FullName}_{methodName}"; if (!_methodCache.TryGetValue(cacheKey, out var methodDelegate)) { // 获取方法信息 var methodInfo = instance.GetType().GetMethod( methodName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); if (methodInfo == null) throw new MissingMethodException($"Method {methodName} not found"); // 创建委托并缓存 methodDelegate = Delegate.CreateDelegate( GetDelegateType(methodInfo), instance, methodInfo); _methodCache.TryAdd(cacheKey, methodDelegate); } // 执行委托 return methodDelegate.DynamicInvoke(args); } // 根据方法签名生成对应的委托类型 private static Type GetDelegateType(MethodInfo methodInfo) { var parameterTypes = methodInfo.GetParameters() .Select(p => p.ParameterType) .ToList(); if (methodInfo.ReturnType == typeof(void)) { return System.Linq.Expressions.Expression.GetActionType(parameterTypes.ToArray()); } else { parameterTypes.Add(methodInfo.ReturnType); return System.Linq.Expressions.Expression.GetFuncType(parameterTypes.ToArray()); } } /// /// 配置初始化。 /// /// /// public void ThreadSettingInit(Tag tag, Action action) { } public void TaskEverythingRun() { var host = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName()); foreach (var ip in host.AddressList) { if (ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) { Console.WriteLine($"ip= {ip.ToString()}"); //new HttpServer(ip.ToString()).HttpServerRun(); //new TcpServer(ip.ToString()); //var server = new EnhancedWebSocketServer($"http://{ip.ToString()}:8809/").StartAsync(); var client = new WebSocketClientWithReconnect($"ws://{ip.ToString()}:8809/socket")/*.StartAsync()*/; //Task.Run(async () => //{ client.StartAsync(); Task.Run(async () => { while (true) { for (int i = 0; i < 10000; i++) { Thread.Sleep(1000); Console.WriteLine($"{DateTime.Now.ToString("HH:mm:ss.fff")}>>>Server{i}"); await client.SendAsync($"Hello Server{i}!"); } } }); //Task.Run(async () => //{ // while (true) // { // for (int i = 60000; i < 70000; i++) // { // Thread.Sleep(1000); // Console.WriteLine($"{DateTime.Now.ToString("HH:mm:ss.fff")}>>>GGG{i}"); // await client.SendAsync($"Hello GGG{i}!"); // } // } //}); //}); break; } } } } public class HttpServer { System.Net.HttpListener HttpSvcHost = null; public static string _listenerPrefix = ""; public HttpServer(string ip) { _listenerPrefix = $"http://{ip}:8807/"; } public void HttpServerRun() { HttpSvcHost = new System.Net.HttpListener(); HttpSvcHost.AuthenticationSchemes = System.Net.AuthenticationSchemes.Anonymous; HttpSvcHost.Prefixes.Add(_listenerPrefix); HttpSvcHost.Start(); HttpSvcHost.BeginGetContext(HttpSvcListenerCallback, null); } private async void HttpSvcListenerCallback(IAsyncResult ar) { System.Net.HttpListenerContext context = null; var data = DateTime.Now; string apth = ""; try { HttpSvcHost.BeginGetContext(HttpSvcListenerCallback, null); context = HttpSvcHost.EndGetContext(ar); System.Net.HttpListenerRequest request = context.Request; System.Net.HttpListenerResponse response = context.Response; using (var reader = new System.IO.StreamReader(request.InputStream, System.Text.Encoding.UTF8)) { string requestJson = reader.ReadToEnd(); System.Net.HttpStatusCode statusCode = 0; apth = request.Url.AbsolutePath; string respstr = HttpSvcListenerCallback_he(request.HttpMethod, request.Url.AbsolutePath, requestJson, out statusCode); string logContent = ""; logContent += $"\r\n[{request.HttpMethod}]{request.Url.AbsolutePath}"; logContent += $"\r\n[request]{requestJson}"; logContent += $"\r\n[response]{respstr}"; _ = Task.Run(() => { LogHelper.Info(logContent); Conn.log默认日志?.Info(logContent); }); byte[] bytstr = Encoding.UTF8.GetBytes(respstr); response.StatusCode = (int)statusCode; response.SendChunked = false; response.ContentLength64 = bytstr.Length; if (request.Url.AbsolutePath.ToLower().Contains(".js")) response.ContentType = "application/javascript"; else if (request.Url.AbsolutePath.ToLower().Contains(".svg")) response.ContentType = "image/svg+xml"; // 异步写入响应 await response.OutputStream.WriteAsync(bytstr, 0, bytstr.Length); } } catch (Exception ex) { _ = Task.Run(() => { Conn.log默认日志.Error(ex.ToString()); }); } finally { context?.Response.Close(); Console.WriteLine(apth + "<<>>" + DateTime.Now.Subtract(data).TotalMilliseconds); } } private System.String HttpSvcListenerCallback_he(System.String method, System.String path, System.String requestJson, out System.Net.HttpStatusCode statusCode) { try { switch (method) { case "POST": { switch (path) { ///任务下发--WMS-->WC case "/api/Wcs/sendTask": { statusCode = System.Net.HttpStatusCode.OK; return JsonConvert.SerializeObject(""); } ///入库异常上报。 WCS-->WMS case "/api/Wcs/stockInException": { statusCode = System.Net.HttpStatusCode.OK; return JsonConvert.SerializeObject(""); } ///任务状态反馈 WCS-->WM case "/api/Wcs/taskFeedback": { statusCode = System.Net.HttpStatusCode.OK; return JsonConvert.SerializeObject(""); } ///入库交互 。WCS-->WMS case "/api/Wcs/stockInInteraction": { statusCode = System.Net.HttpStatusCode.OK; return JsonConvert.SerializeObject(""); } ///任务取消。 WMS-->WCS case "/api/Wcs/cancelTask": { statusCode = System.Net.HttpStatusCode.OK; return JsonConvert.SerializeObject(""); } /// 碟盘机申请任务。 WCS-->WMS case "/api/Wcs/palletStackerInteraction": { statusCode = System.Net.HttpStatusCode.OK; return JsonConvert.SerializeObject(""); } ///修改任务优先级 。 WMS - WCS case "/api/Wcs/modifyTaskPriority": { statusCode = System.Net.HttpStatusCode.OK; return JsonConvert.SerializeObject(""); } /// 输送线任务推送。 WMS-->WCS -- 记录箱号数据,分拣下线后根据想好进入对应的区域。 case "/api/Wcs/putConveyorTask": { statusCode = System.Net.HttpStatusCode.OK; return JsonConvert.SerializeObject(""); } ///上报称重尺寸 - websocket .这里写着作为记录 case "/api/Wcs/reportWeightinfo": { statusCode = System.Net.HttpStatusCode.OK; return JsonConvert.SerializeObject(""); } } break; } case "GET": { switch (path) { case var _ when System.Text.RegularExpressions.Regex.IsMatch(path, @"\.(html|ico|js|css)(\?.*)?$", System.Text.RegularExpressions.RegexOptions.IgnoreCase): { statusCode = System.Net.HttpStatusCode.OK; // 复制到case 上 //var _ when System.Text.RegularExpressions.Regex.IsMatch(path, @"\.(html|ico|js|css)(\?.*)?$", System.Text.RegularExpressions.RegexOptions.IgnoreCase) var filePath = /*Directory.GetCurrentDirectory() + "\\Static" + "\\" + path.Substring(1);*/System.IO.Path.Combine(Directory.GetCurrentDirectory() + "\\Static", path.Substring(1)); return File.ReadAllText(filePath); } } break; } } statusCode = System.Net.HttpStatusCode.NotFound; return ""; } catch (Exception ex) { Conn.log默认日志.Error(ex.ToString()); statusCode = System.Net.HttpStatusCode.InternalServerError; return ""; } } } class EnhancedWebSocketServer { private HttpListener _listener; private readonly string _listenerPrefix; private readonly ConcurrentDictionary _connections = new ConcurrentDictionary(); private CancellationTokenSource _cts = new CancellationTokenSource(); public EnhancedWebSocketServer(string url) { _listenerPrefix = url; } public async Task StartAsync() { try { _listener = new HttpListener(); _listener.Prefixes.Add(_listenerPrefix); _listener.Start(); Console.WriteLine($"WebSocket服务器已启动,监听 {_listenerPrefix}"); while (!_cts.IsCancellationRequested) { HttpListenerContext context = await _listener.GetContextAsync(); if (context.Request.IsWebSocketRequest) { var wsContext = await context.AcceptWebSocketAsync(null); var connectionId = Guid.NewGuid(); _connections[connectionId] = wsContext.WebSocket; _ = HandleConnectionAsync(connectionId, wsContext.WebSocket, _cts.Token); } else { context.Response.StatusCode = 400; context.Response.Close(); } } } catch (Exception ex) when (ex is HttpListenerException || ex is ObjectDisposedException) { // 服务器停止时的正常异常 Console.WriteLine("服务器正在停止..."); } } private async Task HandleConnectionAsync(Guid connectionId, WebSocket webSocket, CancellationToken ct) { var buffer = new byte[1024 * 4]; try { while (webSocket.State == WebSocketState.Open && !ct.IsCancellationRequested) { var result = await webSocket.ReceiveAsync(new ArraySegment(buffer), ct); if (result.MessageType == WebSocketMessageType.Close) { await webSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, "客户端关闭连接", ct); break; } string message = System.Text.Encoding.UTF8.GetString(buffer, 0, result.Count); Console.WriteLine($"连接 {connectionId} 收到消息: {message}"); // 广播消息给所有客户端 await BroadcastMessageAsync($"客户端 {connectionId} 说: {message}"); } } catch (WebSocketException ex) { Console.WriteLine($"连接 {connectionId} 错误: {ex.WebSocketErrorCode} - {ex.Message}"); } catch (Exception ex) { Console.WriteLine($"处理连接 {connectionId} 时出错: {ex.Message}"); } finally { _connections.TryRemove(connectionId, out _); webSocket?.Dispose(); Console.WriteLine($"{DateTime.Now.ToString("HH:mm:ss.fff")} 连接 {connectionId} 已关闭"); } } public async Task BroadcastMessageAsync(string message) { var buffer = System.Text.Encoding.UTF8.GetBytes(message); foreach (var connection in _connections) { if (connection.Value.State == WebSocketState.Open) { try { await connection.Value.SendAsync( new ArraySegment(buffer), WebSocketMessageType.Text, true, CancellationToken.None); } catch (Exception ex) { Console.WriteLine($"广播消息到连接 {connection.Key} 失败: {ex.Message}"); } } } } public async Task StopAsync() { _cts.Cancel(); // 关闭所有连接 foreach (var connection in _connections) { try { if (connection.Value.State == WebSocketState.Open) { await connection.Value.CloseAsync( WebSocketCloseStatus.NormalClosure, "服务器关闭", CancellationToken.None); } connection.Value.Dispose(); } catch (Exception ex) { Console.WriteLine($"关闭连接 {connection.Key} 时出错: {ex.Message}"); } } _listener?.Stop(); _listener?.Close(); Console.WriteLine("WebSocket服务器已停止"); } } public class WebSocketClientWithReconnect { private ClientWebSocket _webSocket; private readonly Uri _serverUri; private readonly CancellationTokenSource _cts = new CancellationTokenSource(); private readonly int _reconnectDelayMs; public event Action OnMessageReceived = msg => Console.WriteLine($"{DateTime.Now.ToString("HH:mm:ss.fff")}>>>Received: {msg}"); public event Action OnConnected = () => Console.WriteLine("Connected to server"); public event Action OnDisconnected = () => Console.WriteLine("Disconnected from server"); public event Action OnError = ex => Console.WriteLine($"Error: {ex.Message}"); public WebSocketClientWithReconnect(string serverUrl, int reconnectDelayMs = 5000) { _serverUri = new Uri(serverUrl); _reconnectDelayMs = reconnectDelayMs; } public async Task StartAsync() { while (!_cts.IsCancellationRequested) { try { _webSocket = new ClientWebSocket(); await _webSocket.ConnectAsync(_serverUri, _cts.Token); OnConnected?.Invoke(); await ReceiveMessagesAsync(); } catch (Exception ex) when (!_cts.IsCancellationRequested) { OnError?.Invoke(ex); await HandleDisconnection(); } } } private async Task ReceiveMessagesAsync() { var buffer = new byte[1024 * 4]; while (_webSocket.State == WebSocketState.Open && !_cts.IsCancellationRequested) { try { var result = await _webSocket.ReceiveAsync(new ArraySegment(buffer), _cts.Token); if (result.MessageType == WebSocketMessageType.Close) { await HandleDisconnection(); break; } var message = System.Text.Encoding.UTF8.GetString(buffer, 0, result.Count); OnMessageReceived?.Invoke(message); } catch (Exception ex) when (!_cts.IsCancellationRequested) { OnError?.Invoke(ex); await HandleDisconnection(); break; } } } private async Task HandleDisconnection() { OnDisconnected?.Invoke(); try { if (_webSocket != null && _webSocket.State != WebSocketState.Closed) { await _webSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, "Closing", CancellationToken.None); _webSocket.Dispose(); } } catch { /* 忽略关闭时的异常 */ } if (!_cts.IsCancellationRequested) { await Task.Delay(_reconnectDelayMs, _cts.Token); } } public async Task StopAsync() { _cts.Cancel(); try { if (_webSocket != null && _webSocket.State == WebSocketState.Open) { await _webSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, "Client shutdown", CancellationToken.None); } } finally { _webSocket?.Dispose(); } } public async Task SendAsync(string message) { if (_webSocket?.State != WebSocketState.Open) { throw new InvalidOperationException("WebSocket is not connected"); } var bytes = System.Text.Encoding.UTF8.GetBytes(message); await _webSocket.SendAsync(new ArraySegment(bytes), WebSocketMessageType.Text, true, _cts.Token); } } public class TcpServer { public static Dictionary TrayIps = new Dictionary(); public TcpServer(string ip) { Init(ip); } private void Init(string ip) { //创建一个新的Socket,这里我们使用最常用的基于TCP的Stream Socket(流式套接字) var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); try { //将该socket绑定到主机上面的某个端口 socket.Bind(new IPEndPoint(IPAddress.Parse(ip), 2025)); Console.WriteLine($"TCPServer socket 监听{ip}:{2025} "); //启动监听,并且设置一个最大的队列长度 socket.Listen(30); //开始接受客户端连接请求 socket.BeginAccept(new AsyncCallback(ClientAccepted), socket); } catch (Exception e) { Console.WriteLine(e.Message); } } public static Dictionary clients = new Dictionary(); public static Dictionary buffers = new Dictionary(); public static void ClientAccepted(IAsyncResult ar) { var socket = ar.AsyncState as Socket; var client = socket.EndAccept(ar); string remote_ip = ((System.Net.IPEndPoint)client.RemoteEndPoint).Address.ToString(); if (clients.Keys.Contains(remote_ip)) { clients[remote_ip] = client; } else { clients.Add(remote_ip, client); } if (!buffers.Keys.Contains(remote_ip)) { buffers.Add(remote_ip, new byte[1024]); } //给客户端发送一个欢迎消息 //client.Send(Encoding.Unicode.GetBytes("Hi there, I accept you request at " + DateTime.Now.ToString())); Console.WriteLine(remote_ip); try { client.BeginReceive(buffers[remote_ip], 0, buffers[remote_ip].Length, SocketFlags.None, new AsyncCallback(ReceiveMessage), client); } catch (Exception ex) { Console.WriteLine($"【接收客户端的消息异常0】:" + ex.Message); } //准备接受下一个客户端请求 socket.BeginAccept(new AsyncCallback(ClientAccepted), socket); } public static void ReceiveMessage(IAsyncResult ar) { try { var socket = ar.AsyncState as Socket; string remote_ip = ((System.Net.IPEndPoint)socket.RemoteEndPoint).Address.ToString(); var length = socket.EndReceive(ar); if (length == 0) { clients.Remove(remote_ip); buffers.Remove(remote_ip); return; } else { if (!clients.Keys.Contains(remote_ip)) { clients.Add(remote_ip, socket); } if (!buffers.Keys.Contains(remote_ip)) { buffers.Add(remote_ip, new byte[1024]); } } try { if (buffers.Keys.Contains(remote_ip)) { //读取出来消息内容 var message = GetHexString(buffers[remote_ip], length); //16 10 // Console.WriteLine($"{DateTime.Now.ToString("hh:mm:ss")} ---> " + remote_ip + " : " + message); //if (message.Substring(0, 4) == "3f00" && message.Substring(message.Length - 4) == "0d0a") //{ // //显示消息 // //string msg = message.Replace(@"0d", "").Replace(@"0a", "").Replace(@"0d0a", "").Trim(); // //PlcHelper.Receive(remote_ip, msg); // //Array.Clear(buffers[remote_ip], 0, buffers[remote_ip].Length);//清空当前IP Buffer //} //else //{ Console.WriteLine($"【TCP信息协议 {DateTime.Now.Millisecond}】:IP:{remote_ip},MSG:{message}"); var mg = message;// Encoding.ASCII.GetString(PlcHelper.Hex2Bin(message)); if (mg.Length > 10) { mg = mg.Substring(0, 10); } Console.WriteLine(mg); if (mg.StartsWith("DK"))//&& mg.Trim().Length == "DK01000024".Length { LogHelper.Info($"扫码器 >{remote_ip} -{mg}"); if (TrayIps.TryGetValue(remote_ip, out string traycode)) { TrayIps[remote_ip] = mg; } else TrayIps.Add(remote_ip, mg); RedisHelper.Add("S扫码器" + (remote_ip.Split('.').LastOrDefault()), mg, out string msg); RedisHelper.Add("S扫码器" + (remote_ip.Split('.').LastOrDefault()) + "#time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), out msg); //Console.WriteLine("TOFF"); //var mst = PlcHelper.Hex2Bin("544F4646"); //TcpServer.TcpServerSend(remote_ip, mst); } //} } else { if (!buffers.Keys.Contains(remote_ip)) { buffers.Add(remote_ip, new byte[1024]); } } } catch (Exception ex) { Console.WriteLine($"【处理客户端的消息异常2】:" + ex.StackTrace); throw; } //接收下一个消息(因为这是一个递归的调用,所以这样就可以一直接收消息了) socket.BeginReceive(buffers[remote_ip], 0, buffers[remote_ip].Length, SocketFlags.None, new AsyncCallback(ReceiveMessage), socket); } catch (Exception ex) { Console.WriteLine(ex.Message); } } private static string GetHexString(byte[] buffer, int lenght) { return BitConverter.ToString(buffer, 0, lenght).Replace("-", string.Empty).ToLower(); } public static bool TcpServerSend(string ip, byte[] msg) { LogHelper.Info($"TcpServerSend >{ip}:{msg}"); if (clients.Keys.Contains(ip)) { var client = clients[ip]; if (client.Connected) { try { client.Send(msg); LogHelper.Info($"TcpServerSend > 发送成功。"); return true; } catch (SocketException ex) { Console.WriteLine(ex.Message); clients[ip].Close(); clients.Remove(ip); } } else { clients[ip].Close(); clients.Remove(ip); } } else { Console.WriteLine("未找到设备的链接:" + ip); } return false; } public static int GetBitdata(int num, int wid) { string bstr = Convert.ToString(num, 2); if (bstr.Length <= wid) { return 0; } return bstr[bstr.Length - wid - 1] - '0'; } public static int SetBinaryDigit(int number, int n, int value) { if (value != 0 && value != 1) throw new ArgumentException("Value must be 0 or 1."); string binaryStr = Convert.ToString(number, 2); // 如果 n 超出当前位数,补 0 扩展 while (binaryStr.Length <= n) { binaryStr = "0" + binaryStr; } // 修改第 n 位(从右往左,最低位是第 0 位) char[] binaryChars = binaryStr.ToCharArray(); binaryChars[binaryChars.Length - 1 - n] = value == 1 ? '1' : '0'; // 转换回十进制 return Convert.ToInt32(new string(binaryChars), 2); } public static List GetStaticClients() => clients.Keys.ToList(); public static Dictionary GetStaticScan() => TrayIps; } public class LogHelper { #region [自定义类][20250323145442478][LogHelper] public static Dictionary loggers = new Dictionary(); public static void Debug(string message, string name = "") { ILogger logger = null; if (loggers.Keys.Contains(name)) { logger = loggers[name]; } else { logger = LogFactory.CreateLogger(name); if (logger != null) { loggers.Add(name, logger); } else { logger = LogFactory.CreateLogger("console"); } } if (logger != null) { logger.Debug(message); } } public static void Info(string message, string name = "") { //logger.Info(message); ILogger logger = null; if (loggers.Keys.Contains(name)) { logger = loggers[name]; } else { logger = LogFactory.CreateLogger(name); if (logger != null) { loggers.Add(name, logger); } else { logger = LogFactory.CreateLogger("infoFile"); } } if (logger != null) { logger.Info(message); } } public static void Error(string message, Exception ex, string name = "") { //logger.Error(ex, message); ILogger logger = null; if (loggers.Keys.Contains(name)) { logger = loggers[name]; } else { logger = LogFactory.CreateLogger(name); if (logger != null && !loggers.Keys.Contains(name)) { loggers.Add(name, logger); } else { logger = LogFactory.CreateLogger("errorFile"); } } if (logger != null) { logger.Error($"{message}{ex.StackTrace}"); } } #endregion [自定义类][20250323145442478][LogHelper] } public class LogFactory { #region [自定义类][20250323145505759][LogFactory] /// /// 通过配置文件配置日志 /// static LogFactory() { var loggerNames = new List() { "HosttoagvTask", "HosttoagvCar", "NDC", "杭奥" }; LogManager.Configuration = DefaultConfig(loggerNames); } public static ILogger CreateLogger(string name) { var logger = LogManager.GetLogger(name); return logger; } public static LoggingConfiguration DefaultConfig(List loggerNames) { var config = new LoggingConfiguration(); loggerNames.ForEach(a => { var target = new FileTarget(); target.ArchiveAboveSize = 1024 * 1024 * 5;//每个文件最大5M target.ArchiveNumbering = ArchiveNumberingMode.DateAndSequence; target.ArchiveFileName = @"${basedir}/Logs/" + a + "/{####}.txt"; target.FileName = @"${basedir}/Logs/" + a + "/${shortdate}.txt";//当前文件路径 target.Layout = @"${longdate} | ${level:uppercase=false:padding=-5} | ${message} ${onexception:${exception:format=tostring} ${newline} ${stacktrace} ${newline}"; config.AddTarget(a, target); config.AddRuleForOneLevel(LogLevel.Info, target, a); }); // 添加target-console var consoleTarget = new ColoredConsoleTarget(); consoleTarget.Layout = @"${longdate} | ${level:uppercase=false:padding=-5} | ${message} ${onexception:${exception:format=tostring} ${newline} ${stacktrace} ${newline}"; config.AddTarget("console", consoleTarget); config.AddRule(LogLevel.Debug, LogLevel.Fatal, consoleTarget); //添加target-info var infoFileTarget = new FileTarget(); infoFileTarget.ArchiveAboveSize = 1024 * 1024 * 5;//每个文件最大5M infoFileTarget.ArchiveNumbering = ArchiveNumberingMode.DateAndSequence; infoFileTarget.ArchiveFileName = @"${basedir}/Logs/Info/{####}.txt"; infoFileTarget.FileName = @"${basedir}/Logs/Info/${shortdate}.txt";//当前文件路径 infoFileTarget.Layout = @"${longdate} | ${level:uppercase=false:padding=-5} | ${message} ${onexception:${exception:format=tostring} ${newline} ${stacktrace} ${newline}"; config.AddTarget("infoFile", infoFileTarget); config.AddRuleForOneLevel(LogLevel.Info, infoFileTarget);//INFO写在Info文件 //添加target-err var errorFileTarget = new FileTarget(); errorFileTarget.ArchiveAboveSize = 1024 * 1024 * 5;//每个文件最大5M errorFileTarget.ArchiveNumbering = ArchiveNumberingMode.DateAndSequence; errorFileTarget.ArchiveFileName = @"${basedir}/Logs/Error/{####}.txt"; errorFileTarget.FileName = @"${basedir}/Logs/Error/${shortdate}.txt"; errorFileTarget.Layout = @"${longdate} | ${level:uppercase=false:padding=-5} | ${message} ${onexception:${exception:format=tostring} ${newline} ${stacktrace} ${newline}"; config.AddTarget("errorFile", errorFileTarget); config.AddRule(LogLevel.Error, LogLevel.Fatal, errorFileTarget); return config; } #endregion [自定义类][20250323145505759][LogFactory] } } #endregion [自定义类-VS][20250701112200484][AutoThread]