Ho appreso da un mio collega l’esistenza del comando unix/linux tail, che com il flag –f monitora costantemente un file e scrive a console le nuove linee aggiunte. Questo è ottimo se si sta seguendo realtime un log su file. L’unico problema di questa soluzione è che – almeno nell ‘ implementazione che abbiamo usato- usando il RollingFileAppender di log4net il file rimaneva locked, e durante la rinomina automatica falliva e si rischiava di perdere comopletamente un segmento di log.
A tale scopo ho scritto una piccola utility C# che fa esattamente la stessa cosa di tail –f, che troavate su codeplex a questo indirizzo.
Una breve descrizione della command line:
- tailf mylog.txt continuously dump on the console the content of mylog.txt as soon new lines are written into it.
- tailf -n:15 mylog.txt continuously dump on the console the content of mylog.txt as soon new lines are written into it. At startup the last 15 lines are dumped.
- tailf mylog.txt -f:ERROR continuously dump on the console the content of mylog.txt as soon new lines are written into it. Just lines containing "ERROR" are shown.
- tailf mylog.txt -f:"ERROR|WARN" continuously dump on the console the content of mylog.txt as soon new lines are written into it. Just lines containing "ERROR" or "WARN" are shown; double quotes are necessary since | is a special char in the command shell.
Per avere invece al volo un idea di come usare il codice per altri scopi date
un’ occhiata qui.