增加工具类

This commit is contained in:
zhangyazhou 2022-02-25 19:32:14 +08:00
parent e0741d6bde
commit 2b6de62eb3
6 changed files with 157 additions and 3 deletions

View File

@ -8,6 +8,8 @@ using System.Threading.Tasks;
using System.Windows;
using log4net;
using log4net.Config;
using WebSocketTool.Util;
using LogManager = WebSocketTool.Util.LogManager;
namespace WebSocketTool
{
@ -16,20 +18,21 @@ namespace WebSocketTool
/// </summary>
public partial class App : Application
{
public static readonly ILog Log = LogManager.GetLogger(nameof(App));
private static Log log;
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
LogManager.GetManager().Init("WebSocketTool");
XmlConfigurator.ConfigureAndWatch(new FileInfo("log4net.config"));
Directory.CreateDirectory("log");
log = LogManager.GetManager().GetLog(nameof(App));
}
public static void RunOnUIThread(Action action)
{
if (Current == null)
{
Log.Info("application current is null");
log.Info("application current is null");
return;
}
if (Current.CheckAccess())

88
WebSocketTool/Util/Log.cs Normal file
View File

@ -0,0 +1,88 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Documents;
using log4net;
namespace WebSocketTool.Util
{
public class LogManager
{
public static readonly Lazy<LogManager> Lazy = new Lazy<LogManager>(() => new LogManager());
private readonly Dictionary<string, Log> logs = new Dictionary<string, Log>();
public static string LogName { get; private set; } = nameof(App);
private LogManager() { }
public static LogManager GetManager()
{
return Lazy.Value;
}
public void Init(string logName)
{
if (!string.IsNullOrEmpty(logName))
{
LogName = logName;
}
}
public Log GetLog(string tag)
{
if (logs.ContainsKey(tag))
{
return logs[tag];
}
else
{
var log = new Log(tag);
logs[tag] = log;
return log;
}
}
}
public class Log
{
private readonly ILog log;
public Log(string tag)
{
log = log4net.LogManager.GetLogger($"[{LogManager.LogName}]:[{tag}]");
}
public void Debug(string msg)
{
log.Debug(msg);
}
public void Info(string msg)
{
log.Info(msg);
}
public void Warn(string msg)
{
log.Warn(msg);
}
public void Error(string msg, Exception e)
{
log.Error(msg, e);
}
public void Error(Exception e)
{
log.Error(e);
}
public void Fatal(string msg, Exception e = null)
{
log.Fatal(msg, e);
}
public void Fatal(Exception e)
{
log.Fatal(e);
}
}
}

View File

@ -0,0 +1,47 @@
using System;
using System.Net;
using System.Text.RegularExpressions;
namespace WebSocketTool.Util
{
public class NetUtil
{
private static readonly Log Log = LogManager.GetManager().GetLog(nameof(NetUtil));
//A类地址10.0.0.0--10.255.255.255
//B类地址172.16.0.0--172.31.255.255
//C类地址192.168.0.0--192.168.255.255
private const string AIp = @"^10\.(1\d{2}|2[0-4]\d|25[0-5]|[1-9]\d|[0-9])\.(1\d{2}|2[0-4]\d|25[0-5]|[1-9]\d|[0-9])\.(1\d{2}|2[0-4]\d|25[0-5]|[1-9]\d|[0-9])$";
private const string BIp = @"^172\.(1[6789]|2[0-9]|3[01])\.(1\d{2}|2[0-4]\d|25[0-5]|[1-9]\d|[0-9])\.(1\d{2}|2[0-4]\d|25[0-5]|[1-9]\d|[0-9])$";
private const string CIp = @"^192\.168\.(1\d{2}|2[0-4]\d|25[0-5]|[1-9]\d|[0-9])\.(1\d{2}|2[0-4]\d|25[0-5]|[1-9]\d|[0-9])$";
public static void GetHostAddress(string url)
{
try
{
var entry = Dns.GetHostAddresses(url);
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
}
public static bool IsInnerIp(string url)
{
var entry = Dns.GetHostAddresses(url);
if (entry == null)
{
return true;
}
var isInner = true;
foreach (var address in entry)
{
var ip = address.ToString();
isInner = isInner && (Regex.IsMatch(ip, AIp) || Regex.IsMatch(ip, BIp) || Regex.IsMatch(ip, CIp));
}
return isInner;
}
}
}

View File

@ -0,0 +1,13 @@
using System;
using log4net.DateFormatter;
namespace WebSocketTool.Util
{
public class TimeUtil
{
public static string GetLogTime()
{
return DateTime.Now.ToString("[yyyy-MM-dd HH:mm:ss.ms]");
}
}
}

View File

@ -67,6 +67,9 @@
<DependentUpon>ClientWindow.xaml</DependentUpon>
</Compile>
<Compile Include="Properties\Annotations.cs" />
<Compile Include="Util\Log.cs" />
<Compile Include="Util\NetUtil.cs" />
<Compile Include="Util\TimeUtil.cs" />
<Compile Include="View\Dialog\Toast.xaml.cs">
<DependentUpon>Toast.xaml</DependentUpon>
</Compile>

Binary file not shown.