C#追加日志文件

2021-01-23 23:12

阅读:455

标签:static   ati   win   file   exist   log   null   creat   using   

追加日志文件

using System;
using System.IO;

class DirAppend
{
    public static void Main()
    {
        using (StreamWriter w = File.AppendText("log.txt"))
        {
            Log("Test1", w);
            Log("Test2", w);
        }
        
        using (StreamReader r = File.OpenText("log.txt"))
        {
            DumpLog(r);
        }
    }

    public static void Log(string logMessage, TextWriter w)
    {
        w.Write("\r\nLog Entry : ");
        w.WriteLine($"{DateTime.Now.ToLongTimeString()} {DateTime.Now.ToLongDateString()}");
        w.WriteLine("  :");
        w.WriteLine($"  :{logMessage}");
        w.WriteLine ("-------------------------------");
    }

    public static void DumpLog(StreamReader r)
    {
        string line;
        while ((line = r.ReadLine()) != null)
        {
            Console.WriteLine(line);
        }
    }
}
// The example creates a file named "log.txt" and writes the following lines to it,
// or appends them to the existing "log.txt" file:

// Log Entry : //  :
//  :Test1
// -------------------------------

// Log Entry : //  :
//  :Test2
// -------------------------------

// It then writes the contents of "log.txt" to the co

C#追加日志文件

标签:static   ati   win   file   exist   log   null   creat   using   

原文地址:https://www.cnblogs.com/icaowu/p/12059442.html


评论


亲,登录后才可以留言!