Exemplo n.º 1
0
 public void RegisterSink(LoggerSinkConfiguration sinkConfiguration)
 {
     if (_sinkOptions.UseStructuredLogging)
     {
         sinkConfiguration.Console(new ElasticsearchJsonFormatter());
     }
     else
     {
         sinkConfiguration.Console();
     }
 }
        private static void LogInitialization()
        {
            string applicationDataDirectory = @"DigitalZenWorks\MusicManager";
            string baseDataDirectory        = Environment.GetFolderPath(
                Environment.SpecialFolder.ApplicationData,
                Environment.SpecialFolderOption.Create) + @"\" +
                                              applicationDataDirectory;

            string       logFilePath    = baseDataDirectory + @"\MusicManager.log";
            const string outputTemplate =
                "[{Timestamp:yyyy-MM-dd HH:mm:ss} {Level:u3}] " +
                "{Message:lj}{NewLine}{Exception}";

            LoggerConfiguration configuration = new ();

            configuration = configuration.MinimumLevel.Verbose();

            LoggerSinkConfiguration sinkConfiguration = configuration.WriteTo;

            sinkConfiguration.Console(LogEventLevel.Verbose, outputTemplate);
            sinkConfiguration.File(
                logFilePath,
                LogEventLevel.Verbose,
                outputTemplate,
                flushToDiskInterval: TimeSpan.FromSeconds(1));
            Serilog.Log.Logger = configuration.CreateLogger();

            LogManager.Adapter =
                new Common.Logging.Serilog.SerilogFactoryAdapter();
        }
Exemplo n.º 3
0
 /// <summary>
 /// Configure standard input/output
 /// </summary>
 /// <param name="configuration"></param>
 /// <param name="formatter"></param>
 /// <param name="restrictedToMinimumLevel"></param>
 /// <param name="stdErrorFromLevel"></param>
 /// <returns><see cref="LoggerConfiguration"/></returns>
 public static LoggerConfiguration ConfigueStd(this LoggerSinkConfiguration configuration,
                                               ITextFormatter formatter,
                                               LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
                                               LogEventLevel?stdErrorFromLevel        = null
                                               )
 {
     return(configuration.Console(
                formatter,
                restrictedToMinimumLevel,
                null,
                stdErrorFromLevel));
 }
Exemplo n.º 4
0
        public static LoggerConfiguration HostLevelLog(this LoggerSinkConfiguration writeTo)
        {
            var template = ConfigConsts.Logging.HostLevelLogTemplate;

#if DEBUG
            return(writeTo.Console(restrictedToMinimumLevel: LogEventLevel.Information, outputTemplate: template));
#else
            return(writeTo.File($"{ConfigConsts.Logging.HostLevelLogFolder}/{ConfigConsts.Logging.HostLevelLogFile}",
                                rollingInterval: RollingInterval.Month,
                                restrictedToMinimumLevel: LogEventLevel.Information, outputTemplate: template));
#endif
        }
Exemplo n.º 5
0
        /// <inheritdoc />
        protected override void Register([NotNull] LoggerSinkConfiguration configuration)
        {
            if (configuration == null)
            {
                throw new ArgumentNullException(nameof(configuration));
            }

            configuration.Console(
                LogEventLevel.Verbose,
                _options.ConsoleMessageTemplate,
                theme: AnsiConsoleTheme.Literate
                );
        }
Exemplo n.º 6
0
        private static void StartUp()
        {
            LoggerConfiguration     configuration     = new LoggerConfiguration();
            LoggerSinkConfiguration sinkConfiguration = configuration.WriteTo;

            sinkConfiguration.Console(LogEventLevel.Verbose, OutputTemplate);
            sinkConfiguration.File(
                LogFilePath, LogEventLevel.Verbose, OutputTemplate);
            Serilog.Log.Logger = configuration.CreateLogger();

            LogManager.Adapter =
                new Common.Logging.Serilog.SerilogFactoryAdapter();
        }
Exemplo n.º 7
0
        public static LoggerConfiguration HostLevelLog(this LoggerSinkConfiguration writeTo)
        {
            var template = LoggingConsts.HostLevelLogTemplate;
            var env      = Environment.GetEnvironmentVariable(EnvironmentVariables.AspNetCoreEnv.Key);

            if (env == EnvironmentVariables.AspNetCoreEnv.Development)
            {
                return(writeTo.Console(restrictedToMinimumLevel: LogEventLevel.Information, outputTemplate: template));
            }

            return(writeTo.File($"{LoggingConsts.HostLevelLogFolder}/{LoggingConsts.HostLevelLogFile}",
                                rollingInterval: RollingInterval.Month,
                                restrictedToMinimumLevel: LogEventLevel.Information, outputTemplate: template));
        }
        /// <summary>
        /// コンソール出力時のフォーマッタを設定する。
        /// </summary>
        /// <param name="loggerSinkConfiguration"></param>
        /// <param name="formatterName?">フォーマッタのクラス名(完全修飾名),アセンブリ名
        /// (例: Serilog.Formatting.Json.JsonFormatter, Serilog)
        /// <para>nullまたはFormatterとして解釈できない不正な文字列を指定した場合、代わりにデフォルトフォーマッタ
        /// (JsonFormatter) を使います。</para>
        /// <example>
        ///   <list type="bullet">
        ///     <item>JsonFormatter</term>
        ///     <description>Serilog.Formatting.Json.JsonFormatter,Serilog</description>
        ///     <item>CompactJsonFormatter</item>
        ///     <description>Serilog.Formatting.Compact.CompactJsonFormatter,Serilog.Formatting.Compact</description>
        ///     <item>ElasticsearchJsonFormatter</item>
        ///     <description>Serilog.Formatting.Elasticsearch.ElasticsearchJsonFormatter,Serilog.Formatting.Elasticsearch</description>
        ///   </list>
        /// </example>
        /// </param>
        /// <returns>LoggerConfiguration</returns>
        public static LoggerConfiguration ConsoleWithFormatter(this LoggerSinkConfiguration loggerSinkConfiguration, string formatterName)
        {
            try
            {
                // formatterName := <ClassName>,<AssemblyName>
                string[] values = formatterName?.Replace(" ", "").Split(",");
                if (values.Length == 2)
                {
                    (string className, string assemblyName) = (values[0], values[1]);

                    // load the assembly
                    var assembly = Assembly.Load(assemblyName);

                    // load the formatter class
                    Type type = assembly?.GetType(className);

                    // create an Instance with all default params
                    ParameterInfo[] paramInfos = type?.GetConstructors()?.FirstOrDefault <ConstructorInfo>()?.GetParameters();
                    object[]        ctorParams = paramInfos?.Select(p => p.DefaultValue)?.ToArray();
                    ITextFormatter  formatter  = Activator.CreateInstance(type, ctorParams) as ITextFormatter;

                    // configure console with the specified formatter
                    if (formatter != null)
                    {
                        return(loggerSinkConfiguration.Console(formatter));
                    }
                }
            }
            catch (Exception e)
            {
                // go thru and use the default formatter if something went wrong
                Console.WriteLine($"wrong log formatter: {formatterName}. The default formatter is used instead. {e.ToString()}");
            }

            // use default Formatter (JsonFormatter)
            return(loggerSinkConfiguration.Console());
        }
Exemplo n.º 9
0
 /// <summary>
 /// Configure standard input/output
 /// </summary>
 /// <param name="configuration"></param>
 /// <param name="outputTemplate"></param>
 /// <param name="theme"></param>
 /// <param name="restrictedToMinimumLevel"></param>
 /// <param name="formatProvider"></param>
 /// <param name="stdErrorFromLevel"></param>
 /// <returns><see cref="LoggerConfiguration"/></returns>
 public static LoggerConfiguration ConfigueStd(this LoggerSinkConfiguration configuration,
                                               string outputTemplate = DefaultOutputTemplate,
                                               ConsoleTheme theme    = null,
                                               LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
                                               IFormatProvider formatProvider         = null,
                                               LogEventLevel?stdErrorFromLevel        = null
                                               )
 {
     return(configuration.Console(
                outputTemplate: outputTemplate,
                formatProvider: formatProvider,
                standardErrorFromLevel: stdErrorFromLevel,
                theme: theme ?? SystemConsoleTheme.Literate,
                restrictedToMinimumLevel: restrictedToMinimumLevel));
 }
Exemplo n.º 10
0
 /// <summary>
 /// Writes log events to <see cref="System.Console"/>, using pretty printing to display inline event data.
 /// </summary>
 /// <param name="sinkConfiguration">Logger sink configuration.</param>
 /// <param name="restrictedToMinimumLevel">The minimum level for
 /// events passed through the sink.</param>
 /// <param name="outputTemplate">A message template describing the format used to write to the sink.
 /// the default is "{Timestamp} [{Level}] {Message}{NewLine}{Exception}".</param>
 /// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param>
 /// <param name="standardErrorFromLevel">Specifies the level at which events will be written to standard error.</param>
 /// <returns>Configuration object allowing method chaining.</returns>
 public static LoggerConfiguration LiterateConsole(
     this LoggerSinkConfiguration sinkConfiguration,
     LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
     string outputTemplate                = DefaultOutputTemplate,
     IFormatProvider formatProvider       = null,
     LogEventLevel?standardErrorFromLevel = null)
 {
     if (sinkConfiguration == null)
     {
         throw new ArgumentNullException(nameof(sinkConfiguration));
     }
     if (outputTemplate == null)
     {
         throw new ArgumentNullException(nameof(outputTemplate));
     }
     return(sinkConfiguration.Console(
                outputTemplate: outputTemplate,
                formatProvider: formatProvider,
                standardErrorFromLevel: standardErrorFromLevel,
                theme: SystemConsoleTheme.Literate,
                restrictedToMinimumLevel: restrictedToMinimumLevel));
 }
Exemplo n.º 11
0
 public static LoggerConfiguration PlatformSink(this LoggerSinkConfiguration configuration) =>
 configuration.Console();
Exemplo n.º 12
0
 public static LoggerConfiguration SerilogConsole(this LoggerSinkConfiguration sinkConfiguration) => sinkConfiguration.Console();
Exemplo n.º 13
0
 private static void GetConsoleLogger(LoggerSinkConfiguration a)
 {
     a.Console(outputTemplate: "{Level:u1}:{Timestamp:yyyyMMdd HH:mm:ss.fff}: {Message:lj}{NewLine}");
 }