Logging to File via .config

557 views
Skip to first unread message

paolo ponzano

unread,
Jan 7, 2016, 5:57:16 AM1/7/16
to Serilog
Hello,
I was trying to log on file via web.config but the sintax

<add key="serilog:write-to:File.pathFormat" value="%BASEDIR%/Logs/Log-{Date}.log" />

doesn't seems to work... If I use Rollingfile everything works but it puts a -01 , -02 and so on at the end if the file

<add key="serilog:write-to:RollingFile.pathFormat" value="%BASEDIR%/Logs/Log-{Date}.log" />

As I've seen there's a sink for File since if I use 

  var log = new LoggerConfiguration().WriteTo.File()

it propose me as a Sink

Thanks 
Paolo

nblum...@nblumhardt.com

unread,
Jan 7, 2016, 3:57:34 PM1/7/16
to Serilog
Hi Paolo,

If the filenames are being suffixed with `-01` etc., that generally means the files are locked by another process, or by another sink within the process.

A few questions that might help:

1. Are you on Windows or Linux?
2. IIS or self-host?
3. Are you calling `LoggerConfiguration.CreateLogger()` only once at application startup?

Cheers!

paolo ponzano

unread,
Jan 8, 2016, 3:34:08 AM1/8/16
to Serilog
Hello!
I'm under Windows, it's IIS and it's called just one time here

protected void Application_Start(object sender, EventArgs e)
        {
            Environment.SetEnvironmentVariable("BASEDIR", AppDomain.CurrentDomain.BaseDirectory);

            var log = new LoggerConfiguration()
                     //.WriteTo.File()
                        .ReadFrom.AppSettings()
                     // .Enrich.With(new UsernameEnricher())
                     .MinimumLevel.Debug()
                     .Filter.With(new KeepAliveFilter())
                     .CreateLogger();

            Log.Logger = log;
            LogManager.LogFactory = new SerilogFactory(log);

            Log.Information(string.Empty);
            Log.Information("==== Application started ====");
            AppHost.Start();
        }

where my SerilogFactory (that's used for logging servicestack info) is

namespace ServiceStack.Logging.Serilog
{
    /// <summary>
    /// ILogFactory that creates an Serilog ILog logger
    /// </summary>
    public class SerilogFactory : ILogFactory
    {
        private readonly ILogger _log;

        /// <summary>
        /// Initializes a new instance of the <see cref="SerilogFactory"/> class.
        /// </summary>
        public SerilogFactory(ILogger log)
        {
            _log = log;
        }

        /// <summary>
        /// Gets the logger.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <returns></returns>
        public ILog GetLogger(Type type)
        {
            return new SerilogLogger(_log.ForContext(type));
        }

        /// <summary>
        /// Gets the logger.
        /// </summary>
        /// <param name="typeName">Name of the type.</param>
        /// <returns></returns>
        public ILog GetLogger(string typeName)
        {
            try
            {
                var type = Type.GetType(typeName);
                return new SerilogLogger(_log.ForContext(type));
            }
            catch (Exception)
            {
                // if the type is not valid, just return a non-context logger
                return new SerilogLogger(_log); 
            }
        }
    }

Thanks

paolo ponzano

unread,
Jan 12, 2016, 8:23:42 AM1/12/16
to Serilog
Hello nblumhardt,
I've spent some time on this weird problem of the file locking...what happens is that I've to start 2 project from solution, one for debugging web part and I for the WPF one.
When it start to log it seems that (randomly , probably depending upon web waking up time) I got 2 instances (even if I only see one) running...one waken up ny wpf request and the other one by visual studio debug.

I've cross checked and I've this scenario

Log.20160112_002.log

2016-01-12 14:02:09.040 +01:00 [Information] ==== Application started ====
2016-01-12 14:03:11.790 +01:00 [Information] ==== Application ended ====
2016-01-12 14:03:11.790 +01:00 [Information] 
2016-01-12 14:04:33.195 +01:00 [Information] ==== Application started  ====74245329

Log_20160112_003.log

2016-01-12 14:04:42.888 +01:00 [Information] ==== Application started  ====900492043

Those numbers are Random.Next() ... what can I do? is this normal?

nblum...@nblumhardt.com

unread,
Jan 12, 2016, 4:51:43 PM1/12/16
to Serilog
Hi Paolo,

Log files aren't the best fit for multi-process scenarios - maybe install a local dev copy of Seq (https://getseq.net - disclaimer, my project) and route the logs there? If the multiple processes don't cause any other trouble then just collecting the logs in a multi-process-friendly viewer is the quick way forward.

HTH!

Nick
Reply all
Reply to author
Forward
0 new messages