.Net将控制台的输出信息存入到日志文件按分钟生成日志文件

.Net将控制台的输出信息存入到日志文件

.Net将控制台的输出信息存入到日志文件,按分钟生成日志文件

因为控制台的输出比较大,在本地调试抓取日志比较困难时,不想用中间件记录日志的时候,可以将控制台的输出存入到指定目录,采用按分钟存储,一个文件存太大了,打不开。

创建MultiTextWriter类

csharp 复制代码
public class MultiTextWriter : TextWriter
{
    private readonly TextWriter _consoleWriter;
    private StreamWriter _fileWriter;
    private string _currentLogFilePath;
    private DateTime _currentLogFileTime;

    public MultiTextWriter(TextWriter consoleWriter, string logDirectory)
    {
        _consoleWriter = consoleWriter;
        _currentLogFileTime = DateTime.Now;
        _currentLogFilePath = GetLogFilePath(logDirectory, _currentLogFileTime);
        Directory.CreateDirectory(logDirectory);
        _fileWriter = new StreamWriter(new FileStream(_currentLogFilePath, FileMode.Append, FileAccess.Write)) { AutoFlush = true };
    }

    public override Encoding Encoding => _consoleWriter.Encoding;

    public override void Write(char value)
    {
        RotateLogFileIfNeeded();
        _consoleWriter.Write(value);
        _fileWriter.Write(value);
    }

    public override void WriteLine(string value)
    {
        RotateLogFileIfNeeded();
        _consoleWriter.WriteLine(value);
        _fileWriter.WriteLine(value);
    }

    public override void Flush()
    {
        _consoleWriter.Flush();
        _fileWriter.Flush();
    }

    private void RotateLogFileIfNeeded()
    {
        var now = DateTime.Now;
        if (now.Minute != _currentLogFileTime.Minute || now.Hour != _currentLogFileTime.Hour || now.Date != _currentLogFileTime.Date)
        {
            _fileWriter.Dispose();
            _currentLogFileTime = now;
            _currentLogFilePath = GetLogFilePath(Path.GetDirectoryName(_currentLogFilePath)!, _currentLogFileTime);
            _fileWriter = new StreamWriter(new FileStream(_currentLogFilePath, FileMode.Append, FileAccess.Write)) { AutoFlush = true };
        }
    }

    private static string GetLogFilePath(string logDirectory, DateTime logTime)
    {
        return Path.Combine(logDirectory, $"console_output_{logTime:yyyyMMdd_HHmm}.log");
    }
}

在Program里直接引用

csharp 复制代码
    partial class Program
    {
        static void Main(string[] args)
        {
            #region Redirect Console Output to File and Console
            var logDirectory = "Logs";
            var multiWriter = new MultiTextWriter(Console.Out, logDirectory);
            Console.SetOut(multiWriter);
            Console.SetError(multiWriter);
            #endregion
            Console.WriteLine("Hello, World!");
        }
    }

查看bin目录下的日志文件

相关推荐
步、步、为营2 小时前
.NET 的 Native AOT 现在是什么样的?
.net
王维志11 小时前
⏱ TimeSpan:C#时间间隔结构
前端·后端·c#·.net
王维志20 小时前
C# 中的 DateTime
开发语言·c#·.net
lishuangquan19871 天前
在ubuntu上使用jenkins部署.net8程序
ubuntu·jenkins·.net
云中小生2 天前
ASP.NET Core MVC修仙指南
.net
步、步、为营3 天前
.NET 如何实现ChatGPT的Stream传输
chatgpt·.net
喵叔哟3 天前
37.【.NET8 实战--孢子记账--从单体到微服务--转向微服务】--扩展功能--增加Github Action
微服务·github·.net
步、步、为营3 天前
.NET8 正式发布, C#12 新变化
ui·c#·.net
伽蓝_游戏3 天前
Unity UI的未来之路:从UGUI到UI Toolkit的架构演进与特性剖析(7)
游戏·ui·unity·架构·c#·游戏引擎·.net