C# Logger 클래스 수동 패키지

2581 단어 c#저널
C# Logger 클래스의 수동 패키지 단일 예제 모드:
/// 
///  
/// 
class Logger
{
    /// 
    ///  
    /// 
    private static readonly Logger logger=new Logger();
    /// 
    ///  
    /// 
    private static string FilePath;
    /// 
    ///  
    /// 
    private static string FileName;
    /// 
    ///  
    /// 
    private Logger() { }
    /// 
    ///  
    /// 
    ///  
    ///  Logger
    public static Logger Create(string name = "log.txt", string directoryName = "")
    {
        var currentDir = AppDomain.CurrentDomain.BaseDirectory;
        FilePath = currentDir+"//"+directoryName;
        FileName = name;
        return logger;
    }
    /// 
    ///  
    /// 
    ///  
    ///  
    public void Write(string content,string type="None",bool useTD=false)
    {
        var allPath = FilePath + "//" + FileName;
        try
        {
            lock(typeof(Logger))
            {
                if (!Directory.Exists(FilePath))
                    Directory.CreateDirectory(FilePath);
                using (var wr = new StreamWriter(path: allPath, append: true))
                {
                    wr.WriteLine("[{0}]:{1}
{2}", type,DateTime.UtcNow, content); } } } catch (Exception e) { throw new Exception(e + " !"); } } /// /// /// /// public void Info(string content)=>Write(content, type:"Info"); /// /// /// /// public void Warn(string content)=>Write(content, type: "Warn"); /// /// /// /// public void Error(string content)=>Write(content, type: "Error"); /// /// /// /// public void Fatal(string content)=>Write(content, type: "Fatal"); /// /// /// /// public void Debug(string content)=>Write(content, type: "Debug"); /// /// /// public void Clear() { var allPath = FilePath + "//" + FileName; File.Delete(allPath); } }

좋은 웹페이지 즐겨찾기