zoukankan      html  css  js  c++  java
  • C# 记录日志

            /// <summary>
            /// 日志部分
            /// </summary>
            /// <param name="fileName"></param>
            /// <param name="type"></param>
            /// <param name="content"></param>
            public static void WriteLogs(string fileName, string type, string content)
            {
                string path = AppDomain.CurrentDomain.BaseDirectory;
                if (!string.IsNullOrEmpty(path))
                {
                    path = AppDomain.CurrentDomain.BaseDirectory + fileName;
                    if (!Directory.Exists(path))
                    {
                        Directory.CreateDirectory(path);
                    }
                    path = path + "\" + DateTime.Now.ToString("yyyyMMdd");
                    if (!Directory.Exists(path))
                    {
                        Directory.CreateDirectory(path);
                    }
                    path = path + "\" + DateTime.Now.ToString("yyyyMMdd") + ".txt";
                    if (!File.Exists(path))
                    {
                        FileStream fs = File.Create(path);
                        fs.Close();
                    }
                    if (File.Exists(path))
                    {
                        StreamWriter sw = new StreamWriter(path, true, System.Text.Encoding.Default);
                        sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + type + "-->" + content);
                        //  sw.WriteLine("----------------------------------------");
                        sw.Close();
                    }
                }
            }
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Diagnostics;
      
    namespace Utils
    {
        /// <summary>
        /// <para> </para>
        ///  常用工具类——系统日志类
        /// <para> ---------------------------------------------------</para>
        /// <para> WriteEventLog:写入系统日志(2个方法重载)</para>
        /// <para> DelEventName:删除日志事件源分类</para>
        /// </summary>
        public class EventLogHelper
        {
            #region 写入系统日志
            /// <summary>
            /// 写入系统日志
            /// </summary>
            /// <param name="EventName">事件源名称</param>
            /// <param name="LogStr">日志内容</param>
            public static void WriteEventLog(string EventName, string LogStr)
            {
                try
                {
                    if (!EventLog.SourceExists(EventName))
                    {
                        EventLog.CreateEventSource(EventName, EventName);
                    }
                    EventLog.WriteEntry(EventName, LogStr);
                }
                catch (Exception)
                {
                }
            }
            /// <summary>
            /// 写入系统日志
            /// </summary>
            /// <param name="EventName">事件源名称</param>
            /// <param name="LogType">日志类型</param>
            /// <param name="LogStr">日志内容</param>
            public static void WriteEventLog(string EventName, string LogStr, EventLogEntryType LogType)
            {
                try
                {
                    if (!EventLog.SourceExists(EventName))
                    {
                        EventLog.CreateEventSource(EventName, EventName);
                    }
                    EventLog.WriteEntry(EventName, LogStr, LogType);
                }
                catch (Exception)
                {
                }
            }
            #endregion
      
            #region 删除日志事件源分类
            /// <summary>
            /// 删除日志事件源分类
            /// </summary>
            /// <param name="EventName">事件源名</param>
            /// <returns></returns>
            public static bool DelEventName(string EventName)
            {
                bool flag = false;
                try
                {
                    if (EventLog.SourceExists(EventName))
                    {
                        EventLog.DeleteEventSource(EventName,".");
                        flag = true;
                    }
                }
                catch (Exception)
                {
                }
                return flag;
            }
            #endregion
        }
    }
    EventLogHelper
  • 相关阅读:
    Python3学习笔记(十七):requests模块
    fiddler(四)、断点(转)
    fiddler(三)、会话框添加显示请求方法栏
    PostgreSQL12同步流复制搭建-同步不生效的问题、主库恢复后,无法与新主库同步问题
    PostgreSQL的count(*) count(1) count(列名)的区别
    CentOS系统日志(转)
    常用PostgreSQL HA(高可用)工具收集
    转-性能优化中CPU、内存、磁盘IO、网络性能的依赖
    PostgreSQL查询数据库中包含某种类型的表有哪些
    PostgreSQL中with和without time zone两者有什么区别
  • 原文地址:https://www.cnblogs.com/Liyuting/p/7058441.html
Copyright © 2011-2022 走看看