我是靠谱客的博主 纯情金毛,最近开发中收集的这篇文章主要介绍基于C#分步式聊天系统的在线视频直播系统 之 FY.Logfiles(日志文件组件),觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

原码下载


using System;

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


namespace FY.Logfiles
{
/// <summary>
/// 日志文件配置类
/// </summary>
public interface IHelper
{
/// <summary>
/// 日志等级
/// </summary>
/// <returns></returns>
int GetLogLevel();

/// <summary>
/// 服务器ID
/// </summary>
/// <returns></returns>
int GetLocalServerId();

/// <summary>
/// 日志文件存放路径
/// </summary>
/// <returns></returns>
string GetLogFilePath();


/// <summary>
/// 日志文件名格式
/// </summary>
/// <returns></returns>
string GetLogFileNameForamt();


/// <summary>
/// 日志文件大小
/// </summary>
/// <returns></returns>
long GetLogFileLength();
}

}


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


namespace FY.Logfiles
{
public enum LogType : int
{
/// <summary>
/// 日志1级,调试信息
/// </summary>
Debug = 1,
/// <summary>
/// 日志2级,成功信息
/// </summary>
Success = 2,
/// <summary>
/// 日志3级,系统日志
/// </summary>
SystemLog = 3,
/// <summary>
/// 日志4级,警告信息
/// </summary>
Warning = 4,
/// <summary>
/// 日志5级,异常
/// </summary>
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
{
/// <summary>
/// 文件日志类库
/// </summary>
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;
}
}


/// <summary>
/// 初使化日志组件
/// </summary>
/// <param name="helper"></param>
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;
}


/// <summary>
/// 写日志
/// </summary>
/// <param name="objId">对象ID</param>
/// <param name="logType">日志等级、日志类型</param>
/// <param name="moduleName">调用模块名称</param>
/// <param name="msg">日志内容</param>
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;
}
}
}


/// <summary>
/// 异常消息日志
/// </summary>
/// <param name="moduleName"></param>
/// <param name="msg"></param>
public static void WriteErrorLog(string moduleName, string msg)
{
WriteLog(0, LogType.Error, moduleName, msg);
}


/// <summary>
/// 
/// </summary>
/// <param name="moduleName"></param>
/// <param name="format"></param>
/// <param name="args"></param>
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));
}
}
}


原码下载

最后

以上就是纯情金毛为你收集整理的基于C#分步式聊天系统的在线视频直播系统 之 FY.Logfiles(日志文件组件)的全部内容,希望文章能够帮你解决基于C#分步式聊天系统的在线视频直播系统 之 FY.Logfiles(日志文件组件)所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(32)

评论列表共有 0 条评论

立即
投稿
返回
顶部