zoukankan      html  css  js  c++  java
  • 简单日志LogHelper

     public static class LogHelper
        {
            //日志存储路径
            private static string LogPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, System.Configuration.ConfigurationManager.AppSettings["LogPath"]);
    
            private static object LogLock = new object();//日志锁
    
            /// <summary>
            /// 添加正常信息
            /// </summary>
            /// <param name="message"></param>
            public static void AddInfo(string message)
            {
                string fileName = DateTime.Now.ToString("yyyyMMdd") + ".txt";//日志名称
                string fullName = Path.Combine(LogPath, fileName);
    
                lock (LogLock)
                {
                    if (!Directory.Exists(LogPath))//如果目录不存在 创建目录
                    {
                        Directory.CreateDirectory(LogPath);
                    }
                    using (var stream = File.AppendText(fullName))
                    {
                        stream.WriteLine(message);
                    }
                    Console.WriteLine(message);
                }
            }
    
            /// <summary>
            /// 添加错误信息
            /// </summary>
            /// <param name="errorMessage"></param>
            public static void AddError(string errorMessage)
            {
                string fileName ="Error_"+ DateTime.Now.ToString("yyyyMMdd") + ".txt";//日志名称
                string fullName = Path.Combine(LogPath, fileName);
    
                lock (LogLock)
                {
                    if (!Directory.Exists(LogPath))//如果目录不存在 创建目录
                    {
                        Directory.CreateDirectory(LogPath);
                    }
                    using (var stream = File.AppendText(fullName))
                    {
                        stream.WriteLine(errorMessage);
                    }
                    Console.WriteLine(errorMessage);
                }
            }
        }
  • 相关阅读:
    cf D. Vessels
    cf C. Hamburgers
    zoj 3758 Singles' Day
    zoj 3777 Problem Arrangement
    zoj 3778 Talented Chef
    hdu 5087 Revenge of LIS II
    zoj 3785 What day is that day?
    zoj 3787 Access System
    判断给定图是否存在合法拓扑排序
    树-堆结构练习——合并果子之哈夫曼树
  • 原文地址:https://www.cnblogs.com/marshhu/p/6780138.html
Copyright © 2011-2022 走看看