c#簡易log
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace database
{
class DebugLog
{
#region common define
private static string LOGFILE = "logfile.log";
private static string APPLOGINFO = "應用程式啟動";
private static string APPLOGCLOSE = "應用程式關閉";
private static string APPLOGSEPARATOR = " ||... ";
private static string APPLOGFOOTER = "--------------------------------------------------";
private static StreamWriter sw;
#endregion
//設定字符集
public static void setEncoing()
{
//sw.Encoding = System.Text.Encoding.Unicode;
}
//建立應用程式日誌
public static void CreateAppLog()
{
if (File.Exists(LOGFILE))
{
sw = File.AppendText(LOGFILE);
}
else
{
sw = File.CreateText(LOGFILE);
}
sw.WriteLine(System.DateTime.Now + APPLOGSEPARATOR + APPLOGINFO);
CloseLog();
}
//建立執行時日誌
public static void CreateLog()
{
if (File.Exists(LOGFILE))
{
sw = File.AppendText(LOGFILE);
}
else
{
sw = File.CreateText(LOGFILE);
}
}
//關閉應用程式日誌
public static void CloseAppLog()
{
CreateLog();
sw.WriteLine(System.DateTime.Now + APPLOGSEPARATOR + APPLOGCLOSE);
sw.WriteLine(APPLOGFOOTER);
sw.Close();
}
//關閉執行時日誌
public static void CloseLog()
{
sw.WriteLine(System.DateTime.Now + APPLOGSEPARATOR + APPLOGCLOSE);
sw.Close();
}
//寫log obj
public static void WriteLog(object obj)
{
string str = obj.ToString();
CreateLog();
if (File.Exists(LOGFILE))
{
sw.WriteLine(System.DateTime.Now + APPLOGSEPARATOR + str);
}
else
{
sw = File.CreateText(LOGFILE);
sw.WriteLine(System.DateTime.Now + APPLOGSEPARATOR + str);
}
sw.Close();
}
//寫log string
public static void WriteLog(string str)
{
CreateLog();
if (File.Exists(LOGFILE))
{
sw.WriteLine(System.DateTime.Now + APPLOGSEPARATOR + str);
}
else
{
sw = File.CreateText(LOGFILE);
sw.WriteLine(System.DateTime.Now + APPLOGSEPARATOR + str);
}
sw.Close();
}
//寫log obj
public static void Debuglog(object ob)
{
WriteLog(ob);
}
//寫錯誤日誌
public static void Debuglog(Exception e)
{
WriteLog(e.Message);
}
}
}
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace database
{
class DebugLog
{
#region common define
private static string LOGFILE = "logfile.log";
private static string APPLOGINFO = "應用程式啟動";
private static string APPLOGCLOSE = "應用程式關閉";
private static string APPLOGSEPARATOR = " ||... ";
private static string APPLOGFOOTER = "--------------------------------------------------";
private static StreamWriter sw;
#endregion
//設定字符集
public static void setEncoing()
{
//sw.Encoding = System.Text.Encoding.Unicode;
}
//建立應用程式日誌
public static void CreateAppLog()
{
if (File.Exists(LOGFILE))
{
sw = File.AppendText(LOGFILE);
}
else
{
sw = File.CreateText(LOGFILE);
}
sw.WriteLine(System.DateTime.Now + APPLOGSEPARATOR + APPLOGINFO);
CloseLog();
}
//建立執行時日誌
public static void CreateLog()
{
if (File.Exists(LOGFILE))
{
sw = File.AppendText(LOGFILE);
}
else
{
sw = File.CreateText(LOGFILE);
}
}
//關閉應用程式日誌
public static void CloseAppLog()
{
CreateLog();
sw.WriteLine(System.DateTime.Now + APPLOGSEPARATOR + APPLOGCLOSE);
sw.WriteLine(APPLOGFOOTER);
sw.Close();
}
//關閉執行時日誌
public static void CloseLog()
{
sw.WriteLine(System.DateTime.Now + APPLOGSEPARATOR + APPLOGCLOSE);
sw.Close();
}
//寫log obj
public static void WriteLog(object obj)
{
string str = obj.ToString();
CreateLog();
if (File.Exists(LOGFILE))
{
sw.WriteLine(System.DateTime.Now + APPLOGSEPARATOR + str);
}
else
{
sw = File.CreateText(LOGFILE);
sw.WriteLine(System.DateTime.Now + APPLOGSEPARATOR + str);
}
sw.Close();
}
//寫log string
public static void WriteLog(string str)
{
CreateLog();
if (File.Exists(LOGFILE))
{
sw.WriteLine(System.DateTime.Now + APPLOGSEPARATOR + str);
}
else
{
sw = File.CreateText(LOGFILE);
sw.WriteLine(System.DateTime.Now + APPLOGSEPARATOR + str);
}
sw.Close();
}
//寫log obj
public static void Debuglog(object ob)
{
WriteLog(ob);
}
//寫錯誤日誌
public static void Debuglog(Exception e)
{
WriteLog(e.Message);
}
}
}
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/16179598/viewspace-524530/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- c#簡易 logC#
- 簡易Dialog封裝封裝
- 利用tcpdump簡易抓取MySQL Query LogTCPMySql
- C#之簡易猜數字遊戲C#遊戲
- C#使用sqlite-net搭建簡易的ORMC#SQLiteORM
- C# 簡易的串列埠監視上位機實現C#串列埠
- SemanticKernel/C#:檢索增強生成(RAG)簡易實踐C#
- C# 簡易影像處理(包括平移,旋轉,翻轉, 裁切)C#
- 簡易ApiAPI
- 簡易方程
- flutter簡易教程Flutter
- nginx簡易教程Nginx
- FastDFS簡易概括AST
- 簡易JSONJSON
- 簡易操作statspack
- 直播系統原始碼,簡易的自定義確認彈框AlertDialog原始碼
- C# OpenFileDialog用法C#
- C#中影象一致性比較的簡易演算法C#演算法
- 短視訊程式開發,簡易的自定義確認彈框AlertDialog
- 簡易版管道模式模式
- Flutter 動畫簡易教程Flutter動畫
- namedtuple簡易實現
- 手寫簡易PromisePromise
- 手寫簡易webpackWeb
- Solidity 簡易教程Solid
- CMake簡易入門
- HBase簡易遷移
- gdb 簡易入門
- syncthing 簡易教程
- 簡易ATM系統
- c# winform 之fontdialogC#ORM
- C# NLog 自用配置C#
- 【型別轉換】使用c#實現簡易的型別轉換(Emit,Expression,反射)型別C#MITExpress反射
- .Net下采用GET/POST/SOAP方式動態呼叫WebService的簡易靈活方法(C#)WebC#
- C# 簡介C#
- c# Log 日誌 以及不列印重複LogC#
- React簡易版老虎機React
- java生成簡易pdf文件Java