FY. Logfiles (log file component) of online live video broadcast system based on C # Step-by-Step Chat System)

Source: Internet
Author: User

Source code download


Using System;

Using System. Collections. Generic;
Using System. Text;


Namespace FY. Logfiles
{
///
/// Log file configuration class
///
Public interface IHelper
{
///
/// Log Level
///
///
Int GetLogLevel ();

///
/// Server ID
///
///
Int GetLocalServerId ();

///
/// Log file storage path
///
///
String GetLogFilePath ();


///
/// Log file name format
///
///
String GetLogFileNameForamt ();


///
/// Log File Size
///
///
Long GetLogFileLength ();
}

}


Using System;
Using System. Collections. Generic;
Using System. Text;


Namespace FY. Logfiles
{
Public enum LogType: int
{
///


/// Log Level 1, debugging information
///
Debug = 1,
///
/// Log Level 2, successful information
///
Success = 2,
///
/// Log Level 3, System Log
///
SystemLog = 3,
///
/// Log Level 4, warning information
///
Warning = 4,
///
/// Log Level 5, exception
///
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
{
///


/// File log class library
///
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;
}
}


///
/// Original log component
///
///
Public static void Init (IHelper helper)
{
Config = helper;
WriteSystemLog ("Log: Init", "***************************** FY. logfiles logs are initially made ***************************");
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. ************************");
_ IsInit = true;
}


///
/// Write logs
///
/// Object ID
/// Log Level and Log Type
/// Call Module name
/// Log Content
Public static void WriteLog (int objId, LogType logType, string moduleName, string msg)
{
Msg = string. Format ("[{0}] server: {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 = 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;
}
}
}


///
/// Exception message log
///
///
///
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 ));
}
}
}


Source code download

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.