You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Gateway/Proxy/LogSetting.cs

130 lines
3.7 KiB
C#

2 years ago
using System;
using System.Collections.Concurrent;
using UMC.Data;
2 years ago
using System.Text;
using System.Net.Sockets;
10 months ago
using System.Net.NetworkInformation;
using UMC.Net;
using System.Threading.Tasks;
2 years ago
10 months ago
namespace UMC.ITME
2 years ago
{
class LogSetting : UMC.Data.DataProvider
{
2 years ago
10 months ago
static int Mtu = Int32.MaxValue;
static LogSetting()
2 years ago
{
10 months ago
// 获取所有网络接口
NetworkInterface[] networkInterfaces = NetworkInterface.GetAllNetworkInterfaces();
2 years ago
10 months ago
foreach (NetworkInterface networkInterface in networkInterfaces)
2 years ago
{
10 months ago
// 确保网络接口已启用并支持IPv4
if (networkInterface.OperationalStatus == OperationalStatus.Up &&
networkInterface.Supports(NetworkInterfaceComponent.IPv4))
2 years ago
{
10 months ago
// 获取并打印MTU
int mtu = networkInterface.GetIPProperties().GetIPv4Properties().Mtu;
if (mtu < Mtu)
{
Mtu = mtu;
}
2 years ago
}
}
2 years ago
2 years ago
}
10 months ago
UdpClient client;
2 years ago
2 years ago
public static LogSetting Instance()
{
if (_Instance == null)
{
_Instance = new LogSetting();
_Instance.LoadConf();
2 years ago
}
2 years ago
return _Instance;
2 years ago
}
2 years ago
static LogSetting _Instance;
2 years ago
2 years ago
bool _isWriter = false;
public virtual bool IsWriter
2 years ago
{
2 years ago
get
2 years ago
{
2 years ago
return _isWriter;
2 years ago
}
}
2 years ago
public void LoadConf()
2 years ago
{
2 years ago
var provider = Reflection.Configuration("assembly")?["Log"];
if (provider != null)
2 years ago
{
2 years ago
var host = provider["host"];
if (String.IsNullOrEmpty(host) == false)
2 years ago
{
10 months ago
var port = Utility.IntParse(provider["port"], 514);
_isWriter = Mtu > 512;
2 years ago
this.client = new UdpClient(host, port);
2 years ago
}
else
{
10 months ago
this.client?.Dispose();
2 years ago
_isWriter = false;
}
}
else
{
10 months ago
this.client?.Dispose();
2 years ago
_isWriter = false;
2 years ago
}
}
10 months ago
uint logIndex = 0;
public virtual async void Write(String[] logs, int offset, int size)
2 years ago
{
10 months ago
var buffers = System.Buffers.ArrayPool<Byte>.Shared.Rent(Mtu);
var h = logIndex++;
String root = logs[0];
for (var i = 1; i < size; i += 2)
2 years ago
{
10 months ago
var log = logs[i + 1];
if (String.IsNullOrEmpty(log) == false)
2 years ago
{
10 months ago
int index = 0;
index += $"<13>{DateTime.Now:u} {root} {logs[i]}[{h}]:".WriteBytes(buffers, index);
for (var c = 0; c < log.Length; c++)
{
index += Encoding.UTF8.GetBytes(log, c, 1, buffers, index);
if (index + 5 > Mtu)
{
break;
}
}
10 months ago
try
{
await this.client.SendAsync(buffers.AsMemory(0, index));
}
catch (Exception ex)
{
Utility.Error("Syslog", ex.ToString());
}
2 years ago
}
}
10 months ago
System.Buffers.ArrayPool<Byte>.Shared.Return(buffers);
2 years ago
}
public static void Instance(LogSetting logSetting, Provider provider)
{
logSetting.Provider = provider;
_Instance = logSetting;
}
}
}