Created
October 5, 2021 06:47
-
-
Save ruyut/f0225d5756567bdfa9d29aaba393056f to your computer and use it in GitHub Desktop.
Serilog 輸出到指令視窗和輸出到檔案的示範
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using Serilog; | |
namespace RuyutSerilogExample | |
{ | |
internal class Program | |
{ | |
public static void Main(string[] args) | |
{ | |
Log.Logger = new LoggerConfiguration() | |
.MinimumLevel.Verbose() // 設定最低顯示層級 預設: Information | |
.WriteTo.Console() // 輸出到 指令視窗 | |
.WriteTo.File("log-.log", | |
rollingInterval: RollingInterval.Day, // 每天一個檔案 | |
outputTemplate: "{Timestamp:HH:mm:ss} [{Level:u5}] {Message:lj}{NewLine}{Exception}" | |
) // 輸出到檔案 檔名範例: log-20211005.log | |
.CreateLogger(); | |
Log.Information("Init Ruyut"); | |
Log.Debug("debug"); | |
int i = 1; | |
Log.Debug("i={i}", i); | |
string s = "test string"; | |
Log.Debug("s={s}", s); | |
try | |
{ | |
throw new Exception("throw test exception"); | |
} | |
catch (Exception e) | |
{ | |
Log.Error("catch exception:{exception}", e); | |
} | |
Log.CloseAndFlush(); // 程式要結束前呼叫 | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment