chore: 初始化平芝126KV总装线 WebApi

This commit is contained in:
yexingqiang
2026-06-08 17:19:25 +08:00
commit 437c251f58
126 changed files with 5439 additions and 0 deletions

113
Tools/AppConfigManage.cs Normal file
View File

@@ -0,0 +1,113 @@
using Logger;
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Configuration;
using System.Xml;
namespace Tools
{
public class AppConfigManage
{
public static Dictionary<string, string> ReadConfigList()
{
Dictionary<string, string> appSettingsDictionary = new Dictionary<string, string>();
var appSettings = ConfigurationManager.AppSettings;
foreach (var key in appSettings.AllKeys)
{
appSettingsDictionary.Add(key, appSettings[key]);
}
return appSettingsDictionary;
}
public static string ReadConfig(string key)
{
return ConfigurationManager.AppSettings[key];
}
public static void DeleteConfig(string key)
{
// 获取配置文件路径
string configFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None).FilePath;
Configuration config = ConfigurationManager.OpenExeConfiguration(configFile);
// 加载xml文件
XmlDocument xDoc = new XmlDocument();
xDoc.Load(configFile);
// 获取appSettings节点
XmlNode xNode = xDoc.SelectSingleNode("//appSettings");
if (xNode != null)
{
// 查找并删除指定的节点
XmlNode toRemove = xNode.SelectSingleNode($"add[@key='{key}']");
if (toRemove != null)
{
xNode.RemoveChild(toRemove);
config.AppSettings.Settings.Remove(key);
}
}
// 保存xml文档
xDoc.Save(configFile);
// 保存配置文件
config.Save(ConfigurationSaveMode.Modified);
// 刷新配置节以便ConfigurationManager使用最新的配置
ConfigurationManager.RefreshSection("appSettings");
}
public static void SaveConfig(string key, string value)
{
try
{
// 获取配置文件路径
string configFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None).FilePath;
Configuration config = ConfigurationManager.OpenExeConfiguration(configFile);
KeyValueConfigurationElement setting = config.AppSettings.Settings[key];
XmlDocument xDoc = new XmlDocument();
xDoc.Load(configFile);//加载xml文件
XmlNode xNode;
XmlElement xElem1;
XmlElement xElem2;
xNode = xDoc.SelectSingleNode("//appSettings");//获取指定的xml子节点
xElem1 = (XmlElement)xNode.SelectSingleNode($"//add[@key='{key}']");//获取子节点中指定的子节点
//如果能获取到节点就修改节点的value值
if (xElem1 != null)
{
xElem1.SetAttribute("value", value);//给节点中的value属性赋值(修改操作)
}
//如果不能获取到节点,就创建节点
else
{
xElem2 = xDoc.CreateElement("add");
xElem2.SetAttribute("key", key);
xElem2.SetAttribute("value", value);
xNode.AppendChild(xElem2);
}
if (setting != null)
{
setting.Value = value;
}
else
{
config.AppSettings.Settings.Add(key, value);
}
xDoc.Save(configFile);//保存xml文档
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");
}
catch (Exception ex)
{
Log.FunError(ex, MethodBase.GetCurrentMethod().Name);
}
}
}
}

96
Tools/Logger.cs Normal file
View File

@@ -0,0 +1,96 @@
using Serilog;
using Serilog.Events;
using Serilog.Formatting;
using System;
using System.IO;
using System.Security.Policy;
namespace Logger
{
public static class Log
{
private static readonly ILogger logger;
public static event Action<string> ShowMsg; // 记录日志事件
static Log()
{
string logpath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $@"Logs\Log_{DateTime.Now:yyyy-MM-dd}.html");
logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.WriteTo.File(new HtmlFormatter(), logpath,
rollingInterval: RollingInterval.Day,
rollOnFileSizeLimit: true,
retainedFileCountLimit: 7,
fileSizeLimitBytes: 1000 * 1024)
.CreateLogger();
}
public static void Error(string message)
{
ShowMsg?.Invoke(message);
logger.Error(message);
}
public static void FunError(Exception ex, string funName)
{
ShowMsg?.Invoke(funName + " — FunErr — " + ex.Message);
logger.Error(funName + " — FunErr — " + ex.Message);
}
public static void Info(string message)
{
ShowMsg?.Invoke(message);
logger.Information(message);
}
public static void Debug(string message)
{
ShowMsg?.Invoke(message);
logger.Debug(message);
}
public static void Warning(string message)
{
ShowMsg?.Invoke(message);
logger.Warning(message);
}
public static void Fatal(string message)
{
ShowMsg?.Invoke(message);
logger.Fatal(message);
}
}
public class HtmlFormatter : ITextFormatter
{
public void Format(LogEvent logEvent, TextWriter output)
{
string levelColor = GetLevelColor(logEvent.Level);
string message = logEvent.RenderMessage();
output.WriteLine($"<div style=\"color: {levelColor};\">");
output.WriteLine($"<p>[{logEvent.Timestamp:yyyy-MM-dd HH:mm:ss}] [{logEvent.Level}] {message}</p>");
output.WriteLine("</div>");
}
private string GetLevelColor(LogEventLevel level)
{
switch (level)
{
case LogEventLevel.Debug:
return "gray";
case LogEventLevel.Information:
return "black";
case LogEventLevel.Warning:
return "orange";
case LogEventLevel.Error:
return "red";
case LogEventLevel.Fatal:
return "purple";
default:
return "black";
}
}
}
}