• 日志记录类


    代码:

     namespace Assist.Log
    {
        public static class DownLog
        {
            private static string FileName = string.Empty;
            private static StreamWriter swLog = null;
            private static readonly string ExtName = "_DownLog.txt";//日志类型
            private static readonly string ConfigLogPath = "LogPath";//日志储存路径
            private static readonly string ConfigLogSaveDate = "LogSaveDay";//储存天数
            private static System.Collections.Generic.Queue<string> LogBuffer;
            private static ThreadStart job = new ThreadStart(WriterLog);
            private static Thread thread = new Thread(job);
    
            /// <summary>
            /// 存放日志目录
            /// </summary>
            private static string _LogPath = null;
            private static string LogPath
            {
                get
                {
                    if (_LogPath == null)
                    {
                        if (ConfigurationManager.AppSettings[ConfigLogPath] != null)
                        {
                            _LogPath = ConfigurationManager.AppSettings[ConfigLogPath];
                        }
                        else
                        {//默认值
                            //_LogPath = AppDomain.CurrentDomain.BaseDirectory + @"log\";//App应用
                            _LogPath = AppDomain.CurrentDomain.BaseDirectory + @"bin\Log\";//Web应用
                        }
                    }
    
                    return _LogPath;
                }
            }
    
            /// <summary>
            /// 日志保存期(天)
            /// </summary>
            private static int _SaveDay = -1;
            private static int SaveDay
            {
                get
                {
                    if (_SaveDay == -1)
                    {
                        if (ConfigurationManager.AppSettings[ConfigLogSaveDate] != null)
                        {
                            _SaveDay = int.Parse(ConfigurationManager.AppSettings[ConfigLogSaveDate]);
                        }
                        else
                        {//默认值
                            _SaveDay = 31;
                        }
                    }
    
                    return _SaveDay;
                }
            }
    
            private static void WriterLog()
            {
                while (true)
                {
                    TextWriter twLog = GetStreamWriter();
                    while (LogBuffer!=null && LogBuffer.Count > 0)
                    {
                        twLog.Write(LogBuffer.Dequeue() + "\r\n");
                        twLog.Flush();
                        //Thread.Sleep(50);
                    }
                    Thread.Sleep(30);
                }
            }
    
    
            private static StreamWriter GetStreamWriter()
            {
                if ((FileName != System.DateTime.Now.ToString("yyyy_MM_dd")) || swLog == null)
                {
                    if ((swLog != null)) swLog.Close();
                    if (!Directory.Exists(LogPath))
                    {
                        Directory.CreateDirectory(LogPath);
                    }
                    FileName = System.DateTime.Now.ToString("yyyy_MM_dd");
                    DelLog(LogPath, SaveDay);
                    try
                    {
                        swLog = new StreamWriter(Path.Combine(LogPath, FileName + ExtName), true, System.Text.Encoding.UTF8, 1024);
                    }
                    catch
                    {
                        if ((swLog != null))
                        {
                            swLog.Close();
                            System.Threading.Thread.Sleep(30);
                            swLog = new StreamWriter(Path.Combine(LogPath, FileName + ExtName), true, System.Text.Encoding.UTF8, 1024);
                        }
                    }
                }
                return swLog;
            }
    
    
            /// <summary>
            /// 系统日志信息记录
            /// </summary>
            /// <param name="Info">记录的内容(用;号分开)</param>
            public static void WriteLog(string Info)
            {
                if (LogBuffer == null)
                {
                    LogBuffer = new System.Collections.Generic.Queue<string>();
                    thread.IsBackground = true;
                    thread.Start();
                }
                if (!string.IsNullOrEmpty(Info))
                {
                    LogBuffer.Enqueue(Info);
                }
            }
            
            /// <summary>
            /// 自动删除过期日志文件
            /// </summary>
            /// <param name="path">路径</param>
            /// <param name="day">超出的天数</param>
            private static void DelLog(string path, double day)
            {
                if (SaveDay > 0)
                {
                    DirectoryInfo folder = new DirectoryInfo(path);
                    FileInfo[] chldFiles = folder.GetFiles("*" + ExtName);
                    foreach (FileInfo chlFile in chldFiles)
                    {
                        try
                        {
                            if (chlFile.Name.Replace(ExtName, "").CompareTo(DateTime.Now.AddDays(-day).ToString("yyyy_MM_dd")) <= 0) chlFile.Delete();
                        }
                        catch { }
                    }
                }
            }
        }
    }

    调用:

    Assist.Log.DownLog.WriteLog(“日志内容,要求string类型”);
  • 相关阅读:
    sqlserver 日期格式化
    CentOS7系统 ansible自动化部署多台服务器部署
    Linux运维跳槽40道面试精华题
    Linux下SVN创建新的项目
    日志切割
    SVN的安装和启动SVN的安装
    jenkins+Gitlab+maven+tomcat实现自动集成、打包、部署
    nginx启动脚本
    nginx如何调用php
    redis常用命令
  • 原文地址:https://www.cnblogs.com/wxh19860528/p/2578178.html
Copyright © 2020-2023  润新知