C# Serilog both in file and console

Install-Package serilog.aspnetcore

 

using Serilog;

namespace ConsoleApp12
{
    internal class Program
    {
        static int idx = 0;
        static void Main(string[] args)
        {
            InitSerialLog();
            Console.WriteLine($"{DateTime.Now},Finished");
            Log.CloseAndFlush();
        }

        private static void InitSerialLog()
        {
            Log.Logger = new LoggerConfiguration()
            .MinimumLevel.Debug()
            .WriteTo.Console(outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff} [{Level:u3}] {Message:lj}{NewLine}{Exception}")
            .WriteTo.File(
            path: Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
            "CollectTest",
            "log_.log"),
            rollingInterval: RollingInterval.Day,
            rollOnFileSizeLimit: true,
            fileSizeLimitBytes: 1024 * 1024 * 1024,
            retainedFileCountLimit: 3000,
            shared: true,
            outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff} [{Level:u3}] {Message:lj}{NewLine}{Exception}"
            ).CreateLogger();

            while (true)
            {
                Log.Information($"{DateTime.Now.ToString("yyyyMMddHHmmssffff")}_Idx:{Interlocked.Increment(ref idx)}_{Guid.NewGuid():N}");
                Thread.Sleep(100);
            }

        }
    }
}

image

 

image

 

image

 

 

Install-Package Serilog
Install-Package Serilog.Sinks.Console
Install-Package Serilog.Sinks.File

 

 

using Serilog;
namespace ConsoleApp13
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Console.OutputEncoding=System.Text.Encoding.UTF8;
            SerilogDemo();
            Console.WriteLine("Hello, World!");
        }

        static void SerilogDemo()
        {
            try
            {
                Log.Logger = new LoggerConfiguration()
                    .WriteTo.Console(outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff} [{Level:u3}] {Message:lj}{NewLine}{Exception}")
                    .WriteTo.File(
                    path:Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
                    "CollectTest2",
                    "log_.log"),
                    rollingInterval:RollingInterval.Day,
                    rollOnFileSizeLimit:true,
                    fileSizeLimitBytes:1024*1024*1024,
                    retainedFileCountLimit:3000,
                    shared:true,
                    outputTemplate:"{Timestamp:yyyy-MM-dd HH:mm:ss.fff} [{Level:u3}] {Message:lj} {NewLine} {Exception}"
                    )
                    .CreateLogger();
                
                Log.Information($"{DateTime.Now},LogLevel.Information,program started!");
                Log.Warning($"{DateTime.Now},LogLevel.Warning,This is warning!");
                Log.Error($"{DateTime.Now},LogLevel.Error,This is error!");

                Console.WriteLine($"{DateTime.Now},Press any key to exit.");
                Console.ReadKey();
            }
            catch (Exception ex)
            { 
                Log.Fatal(ex, $"{DateTime.Now},{ex.Message}");
            }
            finally
            {
                Log.CloseAndFlush();
            }
        }
    }
}

 

 

2026-03-30 23:12:58.644 [INF] 2026-03-30 23:12:58,LogLevel.Information,program started!
2026-03-30 23:12:58.687 [WRN] 2026-03-30 23:12:58,LogLevel.Warning,This is warning!
2026-03-30 23:12:58.689 [ERR] 2026-03-30 23:12:58,LogLevel.Error,This is error!
2026-03-30 23:12:58,Press any key to exit.

 

 

 

image

 

 

 

image

 

posted @ 2026-03-30 22:52  FredGrit  阅读(2)  评论(0)    收藏  举报