C # dummies

Source: Internet
Author: User

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 ();
}

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.