Пример #1
0
        /// <summary>
        /// This configures Serilog with some defaults
        /// Such as adding ProcessID, Thread, AppDomain etc
        /// It is highly recommended that you keep/use this default in your own logging config customizations
        /// </summary>
        public static LoggerConfiguration MinimalConfiguration(
            this LoggerConfiguration logConfig,
            IHostEnvironment hostEnvironment,
            ILoggingConfiguration loggingConfiguration,
            UmbracoFileConfiguration umbracoFileConfiguration)
        {
            global::Serilog.Debugging.SelfLog.Enable(msg => System.Diagnostics.Debug.WriteLine(msg));

            //Set this environment variable - so that it can be used in external config file
            //add key="serilog:write-to:RollingFile.pathFormat" value="%BASEDIR%\logs\log.txt" />
            Environment.SetEnvironmentVariable("BASEDIR", hostEnvironment.MapPathContentRoot("/").TrimEnd("\\"), EnvironmentVariableTarget.Process);
            Environment.SetEnvironmentVariable("UMBLOGDIR", loggingConfiguration.LogDirectory, EnvironmentVariableTarget.Process);
            Environment.SetEnvironmentVariable("MACHINENAME", Environment.MachineName, EnvironmentVariableTarget.Process);

            logConfig.MinimumLevel.Verbose() //Set to highest level of logging (as any sinks may want to restrict it to Errors only)
            .Enrich.WithProcessId()
            .Enrich.WithProcessName()
            .Enrich.WithThreadId()
            .Enrich.WithProperty("ApplicationId", hostEnvironment.GetTemporaryApplicationId())     // Updated later by ApplicationIdEnricher
            .Enrich.WithProperty("MachineName", Environment.MachineName)
            .Enrich.With <Log4NetLevelMapperEnricher>()
            .Enrich.FromLogContext();     // allows us to dynamically enrich

            logConfig.WriteTo.UmbracoFile(
                path: umbracoFileConfiguration.GetPath(loggingConfiguration.LogDirectory),
                fileSizeLimitBytes: umbracoFileConfiguration.FileSizeLimitBytes,
                restrictedToMinimumLevel: umbracoFileConfiguration.RestrictedToMinimumLevel,
                rollingInterval: umbracoFileConfiguration.RollingInterval,
                flushToDiskInterval: umbracoFileConfiguration.FlushToDiskInterval,
                rollOnFileSizeLimit: umbracoFileConfiguration.RollOnFileSizeLimit,
                retainedFileCountLimit: umbracoFileConfiguration.RetainedFileCountLimit
                );

            return(logConfig);
        }
        /// <summary>
        /// This configures Serilog with some defaults
        /// Such as adding ProcessID, Thread, AppDomain etc
        /// It is highly recommended that you keep/use this default in your own logging config customizations
        /// </summary>
        /// <param name="logConfig">A Serilog LoggerConfiguration</param>
        /// <param name="hostingEnvironment"></param>
        /// <param name="loggingConfiguration"></param>
        public static LoggerConfiguration MinimalConfiguration(
            this LoggerConfiguration logConfig,
            IHostingEnvironment hostingEnvironment,
            ILoggingConfiguration loggingConfiguration,
            IConfiguration configuration,
            out UmbracoFileConfiguration umbFileConfiguration)
        {
            global::Serilog.Debugging.SelfLog.Enable(msg => System.Diagnostics.Debug.WriteLine(msg));

            //Set this environment variable - so that it can be used in external config file
            //add key="serilog:write-to:RollingFile.pathFormat" value="%BASEDIR%\logs\log.txt" />
            Environment.SetEnvironmentVariable("BASEDIR", hostingEnvironment.MapPathContentRoot("/").TrimEnd("\\"), EnvironmentVariableTarget.Process);
            Environment.SetEnvironmentVariable("UMBLOGDIR", loggingConfiguration.LogDirectory, EnvironmentVariableTarget.Process);
            Environment.SetEnvironmentVariable("MACHINENAME", Environment.MachineName, EnvironmentVariableTarget.Process);

            logConfig.MinimumLevel.Verbose() //Set to highest level of logging (as any sinks may want to restrict it to Errors only)
            .Enrich.WithProcessId()
            .Enrich.WithProcessName()
            .Enrich.WithThreadId()
            .Enrich.WithProperty(AppDomainId, AppDomain.CurrentDomain.Id)
            .Enrich.WithProperty("AppDomainAppId", hostingEnvironment.ApplicationId.ReplaceNonAlphanumericChars(string.Empty))
            .Enrich.WithProperty("MachineName", Environment.MachineName)
            .Enrich.With <Log4NetLevelMapperEnricher>()
            .Enrich.FromLogContext();     // allows us to dynamically enrich

            //This is not optimal, but seems to be the only way if we do not make an Serilog.Sink.UmbracoFile sink all the way.
            var umbracoFileConfiguration = new UmbracoFileConfiguration(configuration);

            umbFileConfiguration = umbracoFileConfiguration;

            logConfig.WriteTo.UmbracoFile(
                path: umbracoFileConfiguration.GetPath(loggingConfiguration.LogDirectory),
                fileSizeLimitBytes: umbracoFileConfiguration.FileSizeLimitBytes,
                restrictedToMinimumLevel: umbracoFileConfiguration.RestrictedToMinimumLevel,
                rollingInterval: umbracoFileConfiguration.RollingInterval,
                flushToDiskInterval: umbracoFileConfiguration.FlushToDiskInterval,
                rollOnFileSizeLimit: umbracoFileConfiguration.RollOnFileSizeLimit,
                retainedFileCountLimit: umbracoFileConfiguration.RetainedFileCountLimit
                );

            return(logConfig);
        }