程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> 基於C#分步式聊天系統的在線視頻直播系統 之 FY.Logfiles(日志文件組件)

基於C#分步式聊天系統的在線視頻直播系統 之 FY.Logfiles(日志文件組件)

編輯:C#入門知識

原碼下載


using System;

using System.Collections.Generic;
using System.Text;


namespace FY.Logfiles
{
///
/// 日志文件配置類
///

public interface IHelper
{
///
/// 日志等級
///

///
int GetLogLevel();

///
/// 服務器ID
///

///
int GetLocalServerId();

///
/// 日志文件存放路徑
///

///
string GetLogFilePath();


///
/// 日志文件名格式
///

///
string GetLogFileNameForamt();


///
/// 日志文件大小
///

///
long GetLogFileLength();
}

}


using System;
using System.Collections.Generic;
using System.Text;


namespace FY.Logfiles
{
public enum LogType : int
{
///


/// 日志1級,調試信息
///

Debug = 1,
///
/// 日志2級,成功信息
///

Success = 2,
///
/// 日志3級,系統日志
///

SystemLog = 3,
///
/// 日志4級,警告信息
///

Warning = 4,
///
/// 日志5級,異常
///

Error = 5
}
}


using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.IO;
using System.Runtime.CompilerServices;
using System.Collections;


namespace FY.Logfiles
{
///


/// 文件日志類庫
///

public class Log
{
private static IHelper config;
private static ArrayList _logMsg = new ArrayList();
private static bool _isInit = false;
private static DateTime _modifyTime = DateTime.Now;


public static bool IsInit
{
get
{
return _isInit;
}
}


///
/// 初使化日志組件
///

///
public static void Init(IHelper helper)
{
config = helper;
WriteSystemLog("Log::Init", "***************************FY.Logfiles日志初使化***************************");
WriteSystemLog("Log::Init", "GetLocalServerId={0}", helper.GetLocalServerId());
WriteSystemLog("Log::Init", "GetLogFileLength={0}", helper.GetLogFileLength());
WriteSystemLog("Log::Init", "GetLogFileNameForamt={0}", helper.GetLogFileNameForamt());
WriteSystemLog("Log::Init", "GetLogFilePath={0}", helper.GetLogFilePath());
WriteSystemLog("Log::Init", "GetLogLevel={0}", helper.GetLogLevel());
WriteSystemLog("Log::Init", "Name:FY.Logfiles,Version:1.0.0.1,Author:F1,Phone:15988482677,QQ:535550100");
WriteSystemLog("Log::Init", "***************************FY.Logfiles日志初使化結束************************");
_isInit = true;
}


///
/// 寫日志
///

/// 對象ID
/// 日志等級、日志類型
/// 調用模塊名稱
/// 日志內容
public static void WriteLog(int objId, LogType logType, string moduleName, string msg)
{
msg = string.Format("[{0}]號服務器::{1}", config.GetLocalServerId(), msg);
if (_isInit)
{
if (config.GetLogLevel() <= (int)logType)
{
writeLog(objId, logType, moduleName, msg);
}
}
Trace.WriteLine(String.Format("{0} {1}", moduleName, msg));
}


[MethodImpl(MethodImplOptions.Synchronized)]
private static void writeLog(int objId, LogType logType, string moduleName, string msg)
{
string logStr = DateTime.Now.ToString("HH:mm:ss") + " " + String.Format("[{0}] {1} {2}", logType, moduleName, msg);


lock (_logMsg)
{
_logMsg.Add(logStr) ;


long ticks = DateTime.Now.Ticks - _modifyTime.Ticks;


if (_logMsg.Count < 100 && TimeSpan.FromTicks(ticks).TotalSeconds < 10)
{
return;
}


_modifyTime = DateTime.Now;
}
StreamWriter sw = null;
try
{
string filePath = config.GetLogFilePath();
string fileName = DateTime.Now.ToString(config.GetLogFileNameForamt());
string fileFullName = Path.Combine(filePath, fileName + ".log");


if (!Directory.Exists(filePath))
{
Directory.CreateDirectory(filePath);
}
FileInfo fi = new FileInfo(fileFullName);
int i = 1;
if (!fi.Exists)
{
FileStream fs = fi.Create();
fi.Refresh();
fs.Close();
fs = null;
}
while (fi.Length >= config.GetLogFileLength())
{
fileFullName = fileFullName = Path.Combine(filePath, fileName + "(" + i + ").log");
fi = new FileInfo(fileFullName);
if (!fi.Exists)
{
FileStream fs = fi.Create();
fi.Refresh();
fs.Close();
fs = null;
}
i++;
}
sw = fi.AppendText();


lock (_logMsg)
{
foreach (object o in _logMsg)
{
sw.WriteLine(Convert.ToString(o));
}


_logMsg.Clear();
}
}
catch (Exception ex)
{
Trace.WriteLine(String.Format("Log::writeLog {0}", ex.Message));
}
finally
{
if (null != sw)
{
sw.Flush();
sw.Close();
sw = null;
}
}
}


///
/// 異常消息日志
///

///
///
public static void WriteErrorLog(string moduleName, string msg)
{
WriteLog(0, LogType.Error, moduleName, msg);
}


///
///
///

///
///
///
public static void WriteErrorLog(string moduleName, string format, params object[] args)
{
WriteLog(0, LogType.Error, moduleName, string.Format(format, args));
}


public static void WriteWarning(string moduleName, string msg)
{
WriteLog(0, LogType.Warning, moduleName, msg);
}


public static void WriteWarning(string moduleName, string format, params object[] args)
{
WriteLog(0, LogType.Warning, moduleName, string.Format(format, args));
}


public static void WriteSystemLog(string moduleName, string format, params object[] args)
{
WriteLog(0, LogType.SystemLog, moduleName, string.Format(format, args));
}


public static void WriteLog(string moduleName, string msg)
{
WriteLog(0, LogType.Success, moduleName, msg);
}


public static void WriteLog(string moduleName, string format, params object[] args)
{
WriteLog(0, LogType.Success, moduleName, string.Format(format, args));
}


public static void WriteDebugLog(string moduleName, string format, params object[] args)
{
WriteLog(0, LogType.Debug, moduleName, string.Format(format, args));
}
}
}


原碼下載

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved