Small projects are often used recently. if you wantCodeLogs must be used for reliable writing, but common log frameworks, such as enterprise libraries or log4net, need to be configured for half a day. It is also troublesome to customize a function. There is no way to implement a dumb log class on your own. The code is very tricky, but it is easy to modify and has no configuration.
Public Static Class Sblogger
{
Static Sblogger ()
{
_ Logdir = path. Combine (appdomain. currentdomain. basedirectory, " Log " );
If (! Directory. exists (_ logdir ))
{
Directory. createdirectory (_ logdir );
}
}
/// <Summary>
/// Write Error Log
/// </Summary>
/// <Param name = "MSG"> </param>
/// <Param name = "ARGs"> </param>
Public Static Void Error ( String MSG, Params Object [] ARGs)
{
Writelog ( " Error " , MSG, argS );
}
/// <Summary>
/// Write tracking logs
/// </Summary>
/// <Param name = "MSG"> </param>
/// <Param name = "ARGs"> </param>
Public Static Void Trace ( String MSG, Params Object [] ARGs)
{
Writelog ( " Trace " , MSG, argS );
}
Public Static Void Debug ( String MSG, Params Object [] ARGs)
{
Writelog ( " Trace " , MSG, argS );
Console. writeline (MSG, argS );
}
Private Static Void Writelog ( String Type, String MSG, Params Object [] ARGs)
{
String Log = string. Format ( " {0} {1} {2} " + Environment. newline,
Datetime. Now. tostring ( " Yyyy-mm-dd # hh: mm: SS " ),
Type, String. Format (MSG, argS ));
Lock (_ Writemutex)
{
File. appendalltext (getlogpath (), log, encoding. utf8 );
}
}
Private Static String Getlogpath ()
{
String Path = path. Combine (_ logdir, datetime. Now. tostring ( " Yyyy-mm-dd " ) + " . Log " );
Return Path;
}
Private Static String _ Logdir = "" ;
Private Static Object _ Writemutex = New Object ();
}