zoukankan      html  css  js  c++  java
  • C#日志编写

      在一个完整的信息系统里面,日志系统是一个非常重要的功能组成部分。它可以记录下系统所产生的所有行为,并按照某种规范表达出来。我们可以使用日志系统所记录的信息为系统进行排错,优化系统的性能,或者根据这些信息调整系统的行为。在安全领域,日志系统的重要地位尤甚,可以说是安全审计方面最主要的工具之一。

      日志系统概览

      按照系统类型进行区分的话,日志系统可以分为操作系统日志、应用系统日志、安全系统日志等等。每种操作系统的日志都有其自身特有的设计和规范,例如Windows系统的日志通常按照其惯有的应用程序、安全和系统这样的分类方式进行存储,而类似Linux这样的各种Class UNIX系统通常都使用兼容Syslog规范的日志系统。

      下面主要讲述的应用程序的本地日志记录方式,这里用文本文件记录日志;

    具体应用如下:

    编写日志记录类

        public class WsdLogger
        {
            private static object lockobj = new object();
            private string logDirectory;
            private string loggerDate;
            private string loggerFile;
            private string loggerName;
            private string logThisDirectory;
    
            /// <summary>
            ///  创建日志对象
            /// </summary>
            /// <param name="loggerName">日志文件名</param>
            public WsdLogger(string loggerName)
            {
                if (!string.IsNullOrEmpty(loggerName))
                {
                    this.loggerName = loggerName;
                }
                else
                {
                    this.loggerName = "DefaultLogger";
                }
                //创建程序记录日志文件夹
                this.logDirectory = new FileInfo(Assembly.GetExecutingAssembly().GetName().CodeBase.Replace("file:///", string.Empty)).DirectoryName + @"logs";
                if (!Directory.Exists(this.logDirectory))
                {
                    Directory.CreateDirectory(this.logDirectory);
                }
            }
    
            /// <summary>
            /// 写入日志内容
            /// </summary>
            /// <param name="line"></param>
            public void Write(string line)
            {
                try
                {
                    lock (lockobj)
                    {
                        string str = DateTime.Now.ToString("yyyy-MM-dd");
                        if ((this.loggerFile == "") || !str.Equals(this.loggerDate))
                        {
                            this.loggerDate = str;
                            this.logThisDirectory = this.logDirectory + @"" + this.loggerDate;
                            if (!Directory.Exists(this.logThisDirectory))
                            {
                                Directory.CreateDirectory(this.logThisDirectory);
                            }
                            this.loggerFile = this.logThisDirectory + @"" + this.loggerName + ".log";
                        }
                        if (File.Exists(this.loggerFile))
                        {
                            //判断如果超过1M就进行文件分割
                            FileInfo file = new FileInfo(this.loggerFile);
                            if (file.Length > 1048576)
                            {
                                file.CopyTo(this.logThisDirectory + @"" + this.loggerName + DateTime.Now.ToString("hhmmss") + ".log", true);
                                file.Delete();
                            }
                            using (StreamWriter writer = File.AppendText(this.loggerFile))
                                writer.WriteLine(string.Format("{0} {1} {2}", DateTime.Now.ToString("HH:mm:ss:ffff"), Thread.CurrentThread.Name, line));
                        }
    
                        if (!File.Exists(this.loggerFile))
                            using (StreamWriter writer = File.CreateText(this.loggerFile))
                                writer.WriteLine(string.Format("{0} {1} {2}", DateTime.Now.ToString("HH:mm:ss:ffff"), Thread.CurrentThread.Name, line));
                    }
                }
                catch (Exception exception)
                {
                    using (StreamWriter writer2 = File.Exists("log.txt") ? File.AppendText("log.txt") : File.CreateText("log.txt"))
                    {
                        try
                        {
                            writer2.WriteLine(exception.ToString());
                            writer2.Close();
                        }
                        catch
                        {
                        }
                    }
                }
            }
    
            public void Write(string line, params object[] objects)
            {
                this.Write(string.Format(line, objects));
            }
        }            

    调用日志记录部分

            /// <summary>
            /// 记录rfid操作日志
            /// </summary>
            /// <param name="log"></param>
            public static void WriteRfidLog(RFlDLog log, string err)
            {
                WsdLogger lg = new WsdLogger("WsdReceiveService");
                lg.Write(log.WriteLog(err));
            }

    如果有更好的方法,望各位提供。

  • 相关阅读:
    echarts + timeline 显示多个options
    微信如何获取unionid 并且打通微信公众号和小程序
    枚举
    十三、springboot集成定时任务(Scheduling Tasks)
    十二、springboot之web开发之静态资源处理
    十一、springboot之web开发之Filter
    十、springboot之web开发打包生产
    九、springboot整合redis二之缓冲配置
    RedisTemplate使用
    八、springboot整合redis
  • 原文地址:https://www.cnblogs.com/tuqun/p/3889978.html
Copyright © 2011-2022 走看看