C# 記錄日誌

来源:http://www.cnblogs.com/Liyuting/archive/2017/06/21/7058441.html
-Advertisement-
Play Games

using System; using System.Collections.Generic; using System.Text; using System.Diagnostics; namespace Utils { /// <summary> /// <para> </para> /// 常用 ...


        /// <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

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • using System; namespace DotNet.Utilities { /// <summary> /// BaseRandom /// 產生隨機數 /// /// 隨機數管理,最大值、最小值可以自己進行設定。 /// </summary> public class BaseRando ...
  • using System; namespace DotNet.Utilities { /// <summary> /// 時間類 /// 1、SecondToMinute(int Second) 把秒轉換成分鐘 /// </summary> public class TimeHelper { /// ...
  • using System.Text.RegularExpressions; namespace DotNet.Utilities { /// <summary> /// 漢字轉拼音類 /// </summary> public class EcanConvertToCh { //定義拼音區編碼數組 ...
  • 因為沒有AspNetCoreModule所以無法正常運行 http://download.microsoft.com/download/3/8/1/381CBBF3-36DA-4983-BFF3-5881548A70BE/DotNetCore.1.0.4_1.1.1-WindowsHosting.e ...
  • using System.Xml; using System.Data; namespace DotNet.Utilities { /// <summary> /// Xml的操作公共類 /// </summary> public class XmlHelper { #region 欄位定義 /// ...
  • https加密鏈接,在訪問的過程中,可以保護你的隱私,保證你的敏感數據不會被別人偷窺,竊取。如果你的伺服器在境外,使用https,可以有更穩定的訪問體驗。接下來我們看一下ZKEACMS如何配置使用HTTPS。 ...
  • using System; using System.Security.Cryptography; using System.Text; namespace DotNet.Utilities { /// <summary> /// Encrypt 的摘要說明。 /// </summary> publ ...
  • 一、使用線程的理由 1、可以使用線程將代碼同其他代碼隔離,提高應用程式的可靠性。 2、可以使用線程來簡化編碼。 3、可以使用線程來實現併發執行。 二、基本知識 1、進程與線程:進程作為操作系統執行程式的基本單位,擁有應用程式的資源,進程包含線程,進程的資源被線程共用,線程不擁有資源。 2、前臺線程和 ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...