예제 #1
0
        private static void Configue(LoggerSinkConfiguration configuration, SerilogOptions serilogOptions, Action <LoggerSinkConfiguration> configure = null)
        {
            var outputTemplate = serilogOptions.OutputTemplate ?? SerilogExtensions.DefaultOutputTemplate;

            JsonFormatter jsonFormatter = null;

            if (serilogOptions.FormatJson)
            {
                jsonFormatter = new JsonFormatter();
            }

            var obsoletePathFormat = serilogOptions.PathFormat;
            var path = serilogOptions.Path;

            //compatibility
            if (!string.IsNullOrWhiteSpace(obsoletePathFormat) && string.IsNullOrWhiteSpace(path))
            {
                path = obsoletePathFormat;
            }

            if (!string.IsNullOrWhiteSpace(path))
            {
                // whether relative path is used, the root directory is applied by default.
                if (!Path.IsPathRooted(path))
                {
#if NETFULL
                    path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, path);
#else
                    path = Path.Combine(AppContext.BaseDirectory, path);
#endif
                }
            }
            else
            {
                //By default, the 'logs' directory of the root directory.
#if NETFULL
                path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "logs", "log.log");
#else
                path = Path.Combine(AppContext.BaseDirectory, "logs", "log.log");
#endif
            }

            //stdout
            if (serilogOptions.StdOut)
            {
                if (serilogOptions.FormatJson)
                {
                    configuration.ConfigueStd(jsonFormatter);
                }
                else
                {
                    configuration.ConfigueStd(outputTemplate: outputTemplate);
                }
            }

            //file Record
            if (serilogOptions.RollingFile)
            {
                if (serilogOptions.FormatJson)
                {
                    configuration.ConfigueFile(jsonFormatter, path: path);
                }
                else
                {
                    configuration.ConfigueFile(path: path, outputTemplate: outputTemplate);
                }
            }

            //email
            if (serilogOptions.Email != null)
            {
                if (!string.IsNullOrWhiteSpace(serilogOptions.Email.Account) && !string.IsNullOrWhiteSpace(serilogOptions.Email.Password))
                {
                    serilogOptions.Email.NetworkCredentials = new NetworkCredential(serilogOptions.Email.Account, serilogOptions.Email.Password);
                }

                configuration.Email(serilogOptions.Email, outputTemplate: outputTemplate, restrictedToMinimumLevel: LogEventLevel.Warning);
            }

            configure?.Invoke(configuration);
        }