1. CConfiguration. cs
Using System;
Using System. Collections. Specialized;
Using System. Xml;
Using System. Configuration;
Namespace com. lenovo. zhangyuk. logger
{
/// <Summary>
/// Summary of CConfiguration.
/// </Summary>
Public class CConfiguration
{
// Program global configuration (ing of the configuration file content in memory)
Private static NameValueCollection _ configration = new NameValueCollection ();
Private CConfiguration ()
{
}
/// <Summary>
/// Retrieve configuration items
/// </Summary>
/// <Param name = "key"> </param>
/// <Returns> </returns>
Public static string getConfiguration (string key)
{
// If the configuration item is retrieved for the first time, read all the configuration items from the file into the memory; otherwise, the configuration item is retrieved from the memory.
If (_ configration. Count = 0)
{
_ Configration = ConfigurationSettings. receivettings;
}
Return (null! = _ Configration [key])? _ Configration [key]. ToString (). Trim ():"";
}
}
}
2. ILogger. cs
Using System;
Using System. Diagnostics;
Namespace com. lenovo. zhangyuk. logger
{
/// <Summary>
/// Log output level
/// </Summary>
Public enum enumLevel
{
DEBUG,
INFO,
WARN,
ERROR,
FATAL
}
/// <Summary>
/// Log Content, refer to myLogEntryWrittenArgs
/// </Summary>
Public class myEventLogEntry
{
/// <Summary>
/// Log source, used to record the log Source
/// </Summary>
Public string Source = "";
/// <Summary>
/// Log Content
/// </Summary>
Public string Message = "";
/// <Summary>
/// Log Writing Time
/// </Summary>
Public DateTime TimeWritten = DateTime. Now;
/// <Summary>
/// Log type. For details, refer to the EventLogEntryType definition.
/// </Summary>
Public enumLevel Type = enumLevel. INFO;
}
/// <Summary>
/// Parameters of the log external processing function
/// </Summary>
Public class myLogEntryWrittenArgs: EventArgs
{
Public myLogEntryWrittenArgs ()
{
Entry = new myEventLogEntry ();
}
Public myEventLogEntry Entry;
}
/// <Summary>
/// Function declaration of external processing entry for logs
/// </Summary>
Public delegate void myLogEntryWrittenHandler (object sender, myLogEntryWrittenArgs e );
/// <Summary>
///
/// Write logs based on the log output level
///
/// Features:
///
/// 1. You can write logs at the log output level.
///
/// Log output level: DEBUG <INFO <WARN <ERROR <FATAL
///
/// For example, if the log level is INFO, the call to debug () will not be output, and the call to other methods will be output;
/// If the log level is WARN, call debug () and info () will not be output, and call other methods will be output.
///
/// 2. Call the external processing entry when writing logs
///
/// You can add an external processing entry function (for declaration, see myLogEntryWrittenHandler). During log writing, the program
/// Call the external processing entry function in sequence to obtain the log information in the external processing entry function.
/// The observer mode is used here.
///
/// Example:
/// Private static ILogger logger = CLoggerFactory. getLogger ();
/// Public void logEntryWritten (object sender, myLogEntryWrittenArgs e)
///{
/////......
///}
/// Logger. addEntryWrittenHander (new myLogEntryWrittenHandler (this. logEntryWritten ));
///
/// LogEntryWritten () will be called during log writing.
///
/// </Summary>
Public interface ILogger
{
// Write logs
Void debug (string message, string source );
Void info (string message, string source );
Void warn (string message, string source );
Void error (string message, string source );
Void fatal (string message, string source );
// Set and return the log output level
Void setLevel (enumLevel level );
EnumLevel getLevel ();
// Manage external program processing entries
Void addEntryWrittenHander (Delegate handler );
Void clearEntryWrittenHander ();
Void enableEntryWrittenHander (bool enabled );
}
}
3. CLoggerAbstract. cs
Using System;
Using System. IO;
Using System. Diagnostics;
Using System. Collections;
Namespace com. lenovo. zhangyuk. logger
{
/// <Summary>
/// Abstract class of log processing
///
/// Here the inheritance-based template mode is used
/// In CLoggerAbstract, a general operation is implemented for outputting data to various log sources.
/// The Action output to a specific log source is implemented by the virtual function _ writeLog In the subclass.
/// </Summary>
Abstract class CLoggerAbstract: ILogger
{
// HANDLE that triggers the external method when writing logs
Private static ArrayList _ entryWrittenEventHandler = new ArrayList ();
// Whether to trigger an external method when writing logs
Private static bool _ bEnableEntryWritten = true;
// Log output level. The default value is INFO.
Private static enumLevel _ outputLevel = enumLevel. INFO;
// Abstract function. Specific log writing operations are implemented by sub-classes.
Public abstract void _ writeLog (string message, string source, enumLevel type );
/// <Summary>
/// Constructor
/// </Summary>
/// <Param name = "strEventSource"> </param>
/// <Param name = "strEventLog"> </param>
Public CLoggerAbstract (enumLevel level)
{
_ OutputLevel = level;
}
/// <Summary>
/// Set the log output level
/// </Summary>
/// <Param name = "level"> level </param>
Public void setLevel (enumLevel level)
{
_ OutputLevel = level;
}
/// <Summary>
/// Obtain the log output level
/// </Summary>
/// <Returns> level </returns>
Public enumLevel getLevel ()
{
Return _ outputLevel;
}
/// <Summary>
/// Write debugging information
/// </Summary>
/// <Param name = "message"> </param>
Public void debug (string message, string source)
{
If (_ outputLevel <= enumLevel. DEBUG)
Write (message, source, enumLevel. DEBUG );
}
/// <Summary>