Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

307
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda