Files
2026-07-10 15:25:05 +08:00

155 lines
4.9 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using Zhaizj.Framework.Web;
using System.IO;
namespace Zhaizj.Framework.Log
{
/// <summary>
/// 日志配置文件,默认配置文件在 /framework/config/log.config,日志文件在 /framework/log/log.txt 中
/// </summary>
public class LogConfig
{
/// <summary>
/// 日志配置信息(全局缓存)
/// <remarks>
/// logLevel 的值(不区分大小写)none, debug, info, warn, error, fatal, all
/// logFile 和 logProvider 通常不用填写
/// </remarks>
/// <example>
/// 配置文件的格式(一行一条配置,键值之间用冒号分开)。
/// <code>
/// logLevel : info
/// logFile : log/log.txt
/// logProvider : Zhaizj.Framework.Log.FileLogger
/// </code>
/// </example>
/// </summary>
public static readonly LogConfig Instance = new LogConfig();
/// <summary>
/// 日志配置内容
/// </summary>
public Dictionary<String, String> dic = new Dictionary<String, String>();
/// <summary>
/// 构造函数初始化
/// </summary>
private LogConfig()
{
String absPath = getConfigAbsPath();
dic = cfgHelper.Read(absPath);
this.FilePath = getFilePath(dic);
this.Level = getLevel(dic);
this.LoggerImpl = getLoggerImpl(dic);
}
/// <summary>
/// 获取日志级别
/// </summary>
/// <param name="dic"></param>
/// <returns></returns>
private LogLevel getLevel(Dictionary<String, String> dic)
{
String level;
dic.TryGetValue("logLevel", out level);
if (strUtil.IsNullOrEmpty(level)) return getDefaultLevel();
try
{
return (LogLevel)Enum.Parse(typeof(LogLevel), level, true);
}
catch { return getDefaultLevel(); }
}
/// <summary>
/// 获取默认级别
/// </summary>
/// <returns></returns>
private static LogLevel getDefaultLevel()
{
return LogLevel.None;
}
private void loadDefault()
{
this.Level = LogLevel.Debug;
this.FilePath = getAbsoluteLogPath(getFilePath(dic));
}
/// <summary>
/// 获取配置文件路径
/// </summary>
/// <returns></returns>
private static String getConfigAbsPath()
{
String absolutePath = PathHelper.Map(strUtil.Join(cfgHelper.ConfigRoot, "log.config"));
if (!File.Exists(absolutePath))
{
absolutePath = null;
}
return absolutePath;
}
/// <summary>
/// 日志输出配置
/// </summary>
/// <param name="dic"></param>
/// <returns></returns>
private static String getFilePath(Dictionary<String, String> dic)
{
String filePath;
String logPath;
dic.TryGetValue("logFile", out filePath);
dic.TryGetValue("logPath", out logPath);
filePath = getDefaultLogPath(filePath, logPath);
return getAbsoluteLogPath(filePath);
}
/// <summary>
/// 日志输出路径修改
/// </summary>
/// <param name="filePath"></param>
/// <param name="logdir"></param>
/// <returns></returns>
private static String getDefaultLogPath(string filePath, string logdir)
{
if (!Directory.Exists(logdir))
{
Directory.CreateDirectory(logdir);
}
string logPath = logdir + "/" + DateTime.Now.ToString(filePath) + ".txt";
return cfgHelper.FrameworkRoot + logPath;
}
private static String getAbsoluteLogPath(String path)
{
if (path.StartsWith(cfgHelper.FrameworkRoot) == false)
path = strUtil.Join(cfgHelper.FrameworkRoot, path);
return PathHelper.Map(path);
}
private static String getLoggerImpl(Dictionary<String, String> dic)
{
String logProvider;
dic.TryGetValue("logProvider", out logProvider);
return logProvider;
}
//---------------------------------------------------------------------------------------------
/// <summary>
/// 记录的层次,不区分大小写,有 none, debug, info, warn, error, fatal, all 这几种可选
/// </summary>
public LogLevel Level { get; set; }
/// <summary>
/// 日志文件存储的路径
/// </summary>
public String FilePath { get; set; }
/// <summary>
/// 日志记录工具,默认是 FileLogger
/// </summary>
public String LoggerImpl { get; set; }
}
}