Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

306
Views
Separate log file for separate thread using serilog

I have a .Net Worker Service app which has multiple threads. I want to log each thread into separate file to make it easier to read the logs. Any ideas to implement this?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

A common way of deciding which sink to write to at run-time is to use the Serilog.Sinks.Map:

Log.Logger = new LoggerConfiguration()
    .WriteTo.Map(_ => Thread.CurrentThread.ManagedThreadId,
        (threadId, wt) => wt.File($"log-{threadId}.log"))
.CreateLogger();

Log.Information("Hello from the main thread");

var task1 = Task.Run(() => Log.Information("Hello from thread X"));
var task2 = Task.Run(() => Log.Information("Hello from thread Y"));

Task.WaitAll(task1, task2);

Log.CloseAndFlush();

You should consider limiting the number of open sinks when using this approach.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!