1、背景
项目程序用于记录程序运行状态的日志。包含运行信息,异常错误等。
2、程序源码
/// <summary>
/// 通用日志记录帮助类
/// 创建人:gyc
/// 创建事件:2022-03-17
/// 说明:采用反汇编技术,解析Wesun.Utils.LogHelper.dll获得
/// 根目录LogHelper.config用于设置日志等级。
/// </summary>
public static class LogHelper
{
private static string _configFile = AppDomain.CurrentDomain.BaseDirectory + "LogHelper.config";
private static string _logDir = AppDomain.CurrentDomain.BaseDirectory + "log";
private static string _loglevel = LogHelper.GetValue("loglevel", "error");
public static void Error(string msg)
{
LogHelper.Error(msg, "");
}
public static void Error(string msg, string locate)
{
try
{
string text = LogHelper._logDir + "\\LogService" + DateTime.Now.ToString("yyyy-MM-dd") + ".log";
LogHelper.CreateLogDir();
LogHelper.CreateLogFile(text);
string text2 = string.Format("[{0}]Error {1}:{2}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), locate, msg);
File.AppendAllLines(text, new string[]
{
text2
});
}
catch
{
}
}
public static void Info(string msg)
{
LogHelper.Info(msg, "");
}
public static void Info(string msg, string locate)
{
try
{
string text = LogHelper._logDir + "\\LogService" + DateTime.Now.ToString("yyyy-MM-dd") + ".log";
if (LogHelper._loglevel.Equals("info", StringComparison.OrdinalIgnoreCase) || LogHelper._loglevel.Equals("debug", StringComparison.OrdinalIgnoreCase))
{
LogHelper.CreateLogDir();
LogHelper.CreateLogFile(text);
string text2 = string.Format("[{0}]Info {1}:{2}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), locate, msg);
File.AppendAllLines(text, new string[]
{
text2
});
}
}
catch
{
}
}
public static void Debug(string msg)
{
LogHelper.Debug(msg, "");
}
public static void Debug(string msg, string locate)
{
try
{
string text = LogHelper._logDir + "\\LogService" + DateTime.Now.ToString("yyyy-MM-dd") + ".log";
if (LogHelper._loglevel.Equals("debug", StringComparison.OrdinalIgnoreCase))
{
LogHelper.CreateLogDir();
LogHelper.CreateLogFile(text);
string text2 = string.Format("[{0}]Debug {1}:{2}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), locate, msg);
File.AppendAllLines(text, new string[]
{
text2
});
}
}
catch
{
}
}
private static void CreateLogDir()
{
try
{
if (!Directory.Exists(LogHelper._logDir))
{
Directory.CreateDirectory(LogHelper._logDir);
}
}
catch
{
}
}
private static void CreateLogFile(string a_strlogFile)
{
try
{
if (!File.Exists(a_strlogFile))
{
using (new FileStream(a_strlogFile, FileMode.Create))
{
}
}
}
catch
{
}
}
private static string GetValue(string key)
{
string result;
try
{
string text = AppDomain.CurrentDomain.BaseDirectory + "app.config";
if (!File.Exists(text))
{
string contents = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n<configuration>\r\n<appSettings> \r\n<add key=\"loglevel\" value=\"error\" />\r\n</appSettings>\r\n</configuration>";
File.WriteAllText(text, contents);
}
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(text);
string xpath = "//add [@key=\"" + key + "\"]/@value";
XmlNode xmlNode = xmlDocument.SelectSingleNode(xpath);
result = xmlNode.Value;
}
catch
{
result = "";
}
return result;
}
private static string GetValue(string key, string valueDefault)
{
string text = "";
string result;
try
{
text = LogHelper.GetValue(key);
}
catch
{
result = valueDefault;
return result;
}
if (text == "")
{
result = valueDefault;
}
else
{
result = text;
}
return result;
}
}
2、使用方式
①项目生成目录下,找到LogHelper.config文件,设置日志记录等级
<add key="loglevel" value="info" />
②项目中使用
?LogHelper.Info(msg);
|