Esempio n. 1
0
        /// <summary>
        /// Adds a console logger named 'Console' to the factory.
        /// </summary>
        /// <param name="builder">The <see cref="ILoggingBuilder"/> to use.</param>
        public static ILoggingBuilder AddConsole(this ILoggingBuilder builder)
        {
            builder.AddConfiguration();

            builder.AddConsoleFormatter <JsonConsoleFormatter, JsonConsoleFormatterOptions>();
            builder.AddConsoleFormatter <SystemdConsoleFormatter, ConsoleFormatterOptions>();
            builder.AddConsoleFormatter <SimpleConsoleFormatter, SimpleConsoleFormatterOptions>();

            builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton <ILoggerProvider, ConsoleLoggerProvider>());
            LoggerProviderOptions.RegisterProviderOptions <ConsoleLoggerOptions, ConsoleLoggerProvider>(builder.Services);

            return(builder);
        }
        public static ILoggingBuilder AddOtelJsonConsole(this ILoggingBuilder builder)
        {
            builder.AddConsoleFormatter <OtelJsonConsoleFormatter, OtelJsonConsoleFormatterOptions>();
            builder.AddConsole(options => options.FormatterName = "oteljson");

            return(builder);
        }
        public static ILoggingBuilder AddConsoleFormatter <[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] TFormatter, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] TOptions>(this ILoggingBuilder builder, Action <TOptions> configure)
            where TOptions : ConsoleFormatterOptions
            where TFormatter : ConsoleFormatter
        {
            ThrowHelper.ThrowIfNull(configure);

            builder.AddConsoleFormatter <TFormatter, TOptions>();
            builder.Services.Configure(configure);
            return(builder);
        }
Esempio n. 4
0
        /// <summary>
        /// Adds a custom console logger formatter 'TFormatter' to be configured with options 'TOptions'.
        /// </summary>
        /// <param name="builder">The <see cref="ILoggingBuilder"/> to use.</param>
        /// <param name="configure">A delegate to configure options 'TOptions' for custom formatter 'TFormatter'.</param>
        public static ILoggingBuilder AddConsoleFormatter <[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] TFormatter, TOptions>(this ILoggingBuilder builder, Action <TOptions> configure)
            where TOptions : ConsoleFormatterOptions
            where TFormatter : ConsoleFormatter
        {
            if (configure == null)
            {
                throw new ArgumentNullException(nameof(configure));
            }

            builder.AddConsoleFormatter <TFormatter, TOptions>();
            builder.Services.Configure(configure);
            return(builder);
        }
Esempio n. 5
0
        /// <inheritdoc/>
        public void Configure(HostBuilderContext hostContext, ILoggingBuilder logging)
        {
            logging.ClearProviders();
            logging.AddConsoleFormatter <CustomConsoleFormatter, CustomConsoleFormatterOptions>();
            logging.AddConsole(opt => opt.FormatterName = CustomConsoleFormatter.FormatterName);

            var serilogConfiguration = hostContext.Configuration.GetSection("Logging").GetSection("Serilog");

            if (serilogConfiguration != null)
            {
                logging.AddFile(serilogConfiguration);
            }
        }
Esempio n. 6
0
        /// <summary>
        /// Adds a custom console logger formatter 'TFormatter' to be configured with options 'TOptions'.
        /// </summary>
        /// <param name="builder">The <see cref="ILoggingBuilder"/> to use.</param>
        /// <param name="configure">A delegate to configure options 'TOptions' for custom formatter 'TFormatter'.</param>
        public static ILoggingBuilder AddConsoleFormatter <TFormatter, TOptions>(this ILoggingBuilder builder, Action <TOptions> configure)
            where TOptions : ConsoleFormatterOptions
            where TFormatter : ConsoleFormatter
        {
            if (configure == null)
            {
                throw new ArgumentNullException(nameof(configure));
            }

            builder.AddConsoleFormatter <TFormatter, TOptions>();
            builder.Services.Configure(configure);
            return(builder);
        }
        /// <summary>
        /// 添加Json输出的日志(方便于容器日志采集)
        /// </summary>
        /// <param name="builder"></param>
        /// <param name="options"></param>
        /// <returns></returns>
        public static ILoggingBuilder AddSpJsonConsole(this ILoggingBuilder builder, Action <JsonConsoleFormatterOptions> options = null)
        {
            options ??= _ =>
            {
                _.UseUtcTimestamp = false;
                _.TimestampFormat = "yyyy-MM-dd HH:mm:ss";
            };

            //添加控制台输出
            builder.AddConsoleFormatter <SpJsonConsole, JsonConsoleFormatterOptions>(_ => options(_));

            builder.AddConsole(o => {
                o.FormatterName = "json";
            });
            return(builder);
        }
Esempio n. 8
0
 internal static ILoggingBuilder AddMyLabFormatter(this ILoggingBuilder loggingBuilder, TextWriter debugWriter)
 {
     return(loggingBuilder.AddConsoleFormatter <MyLabConsoleFormatter, MyLabFormatterOptions>(o => o.DebugWriter = debugWriter));
 }
Esempio n. 9
0
 public static ILoggingBuilder AddColoredConsoleFormatter([NotNull] this ILoggingBuilder thisValue, [NotNull] Action <ColoredConsoleFormatterOptions> configure)
 {
     return(thisValue.AddConsoleFormatter <ColoredConsoleFormatter, ColoredConsoleFormatterOptions>(configure));
 }
Esempio n. 10
0
 private static void ConfigureLogging(HostBuilderContext hostContext, ILoggingBuilder logging)
 {
     logging.AddConfiguration(hostContext.Configuration.GetSection("Logging"));
     logging.AddConsoleFormatter <PlainConsoleFormatter, PlainConsoleFormatterOptions>();
     logging.AddConsole(options => options.FormatterName = nameof(PlainConsoleFormatter));
 }