Esempio n. 1
0
        /// <summary>
        /// Writes log events to <see cref="System.Console"/>.
        /// </summary>
        /// <param name="sinkConfiguration">Logger sink configuration.</param>
        /// <param name="restrictedToMinimumLevel">The minimum level for
        /// events passed through the sink. Ignored when <paramref name="levelSwitch"/> is specified.</param>
        /// <param name="levelSwitch">A switch allowing the pass-through minimum level
        /// to be changed at runtime.</param>
        /// <param name="outputTemplate">A message template describing the format used to write to the sink.
        /// the default is <code>"[{Timestamp:HH:mm:ss} {Level:u3}] {Message}{NewLine}{Exception}"</code>.</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>
        /// <param name="theme">The theme to apply to the styled output. If not specified,
        /// uses <see cref="SystemConsoleTheme.Literate"/>.</param>
        /// <returns>Configuration object allowing method chaining.</returns>
        public static LoggerConfiguration Console(
            this LoggerSinkConfiguration sinkConfiguration,
            LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
            string outputTemplate                = DefaultConsoleOutputTemplate,
            IFormatProvider formatProvider       = null,
            LoggingLevelSwitch levelSwitch       = null,
            LogEventLevel?standardErrorFromLevel = null,
            ConsoleTheme theme = null)
        {
            if (sinkConfiguration == null)
            {
                throw new ArgumentNullException(nameof(sinkConfiguration));
            }
            if (outputTemplate == null)
            {
                throw new ArgumentNullException(nameof(outputTemplate));
            }

            var appliedTheme = System.Console.IsOutputRedirected || System.Console.IsErrorRedirected ?
                               ConsoleTheme.None :
                               theme ?? SystemConsoleThemes.Literate;

            var formatter = new OutputTemplateRenderer(appliedTheme, outputTemplate, formatProvider);

            return(sinkConfiguration.Sink(new ConsoleSink(appliedTheme, formatter, standardErrorFromLevel), restrictedToMinimumLevel, levelSwitch));
        }
Esempio n. 2
0
        /// <summary>
        /// Writes log events to <see cref="System.Console"/>.
        /// </summary>
        /// <param name="sinkConfiguration">Logger sink configuration.</param>
        /// <param name="restrictedToMinimumLevel">The minimum level for
        /// events passed through the sink. Ignored when <paramref name="levelSwitch"/> is specified.</param>
        /// <param name="outputTemplate">A message template describing the format used to write to the sink.
        /// The default is <code>"[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}"</code>.</param>
        /// <param name="syncRoot">An object that will be used to `lock` (sync) access to the console output. If you specify this, you
        /// will have the ability to lock on this object, and guarantee that the console sink will not be about to output anything while
        /// the lock is held.</param>
        /// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param>
        /// <param name="levelSwitch">A switch allowing the pass-through minimum level
        /// to be changed at runtime.</param>
        /// <param name="standardErrorFromLevel">Specifies the level at which events will be written to standard error.</param>
        /// <param name="theme">The theme to apply to the styled output. If not specified,
        /// uses <see cref="DefaultKonsoleTheme.Literate"/>.</param>
        /// <param name="applyThemeToRedirectedOutput">Applies the selected or default theme even when output redirection is detected.</param>
        /// <returns>Configuration object allowing method chaining.</returns>
        public static LoggerConfiguration Konsole(
            this LoggerSinkConfiguration sinkConfiguration,
            LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
            string outputTemplate                = DefaultConsoleOutputTemplate,
            IFormatProvider formatProvider       = null,
            LoggingLevelSwitch levelSwitch       = null,
            LogEventLevel?standardErrorFromLevel = null,
            KonsoleTheme theme = null,
            bool applyThemeToRedirectedOutput = false,
            object syncRoot = null,
            ConcurrentWriter concurrentWriter = null)
        {
            if (sinkConfiguration == null)
            {
                throw new ArgumentNullException(nameof(sinkConfiguration));
            }
            if (outputTemplate == null)
            {
                throw new ArgumentNullException(nameof(outputTemplate));
            }

            concurrentWriter ??= new Window(Window.OpenBox("Logs")).Concurrent();

            var appliedTheme = !applyThemeToRedirectedOutput && (System.Console.IsOutputRedirected || System.Console.IsErrorRedirected) ?
                               KonsoleTheme.None :
                               theme ?? DefaultKonsoleThemes.Literate;

            syncRoot ??= DefaultSyncRoot;

            var formatter = new OutputTemplateRenderer(appliedTheme, outputTemplate, formatProvider, concurrentWriter);

            return(sinkConfiguration.Sink(new KonsoleSink(appliedTheme, formatter, standardErrorFromLevel, syncRoot, concurrentWriter), restrictedToMinimumLevel, levelSwitch));
        }
        public Task WriteAsync(ILogPayload payload, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (payload != null)
            {
                var legalityEvents = LogEventSinkFilter.Filter(payload, _sinkConfiguration).ToList();
                var ix             = 0;
                var count          = legalityEvents.Count;

                foreach (var logEvent in legalityEvents)
                {
                    var strategyWrappers = NavigationFilterProcessor.GetValues(logEvent.StateNamespace);

                    if (strategyWrappers == null || !strategyWrappers.Any())
                    {
                        continue;
                    }

                    var targetMessageBuilder = new StringBuilder();
                    using (var output = new StringWriter(targetMessageBuilder, _formatProvider)) {
                        logEvent.RenderMessage(output, _sinkConfiguration.Rendering, _formatProvider);
                    }

                    foreach (var strategy in strategyWrappers.Select(x => x.SavingStrategy))
                    {
                        var targetFilePath = strategy.CheckAndGetFilePath(logEvent);
                        if (string.IsNullOrWhiteSpace(targetFilePath))
                        {
                            continue;
                        }

                        //判断是否需要渲染 extra properties
                        //检查token

                        //渲染OutputTemplate
                        var stringBuilder = new StringBuilder();
                        using (var output = new StringWriter(stringBuilder, _formatProvider)) {
                            OutputTemplateRenderer.Render(strategy.FormattingStrategy.OutputTemplate, output, logEvent, targetMessageBuilder);
                        }

                        //写文件
                        if (_fileAstronautCache.TryGetFileAstronaut(strategy, targetFilePath, out var astronaut))
                        {
                            using (FileAstronautRemover.UsingRegister(targetFilePath, astronaut)) {
                                astronaut.Save(stringBuilder);
                                //astronaut. ...
                                Console.WriteLine("渲染模板为:" + strategy.FormattingStrategy.OutputTemplate.Text);
                                Console.WriteLine("原始结果为:" + targetMessageBuilder);
                                Console.WriteLine("渲染结果为:" + stringBuilder);
                            }
                        }
                    }
                }
            }
#if NET451
            return(Task.FromResult(true));
#else
            return(Task.CompletedTask);
#endif
        }
        public void Test1()
        {
            var theme     = DefaultThemes.LINQPadLiterate;
            var formatter = new OutputTemplateRenderer(theme, DefaultConsoleOutputTemplate, null);
            var sink      = new LINQPadSink(theme, formatter);

            var msg = new MessageTemplate("Hello World", new MessageTemplateToken[0]);
            var evt = new LogEvent(DateTimeOffset.UtcNow, LogEventLevel.Debug, null, msg, new LogEventProperty[0]);

            sink.Emit(evt);
        }
        /// <summary>
        /// Writes log events to the browser console.
        /// </summary>
        /// <param name="sinkConfiguration">Logger sink configuration.</param>
        /// <param name="restrictedToMinimumLevel">The minimum level for
        /// events passed through the sink. Ignored when <paramref name="levelSwitch"/> is specified.</param>
        /// <param name="outputTemplate">A message template describing the format used to write to the sink.
        /// The default is <code>"[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}"</code>.</param>
        /// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param>
        /// <param name="levelSwitch">A switch allowing the pass-through minimum level
        /// to be changed at runtime.</param>
        /// <param name="jsRuntime">An instance of <see cref="IJSRuntime"/> to interact with the browser.</param>
        /// <returns>Configuration object allowing method chaining.</returns>
        public static LoggerConfiguration BrowserConsole(
            this LoggerSinkConfiguration sinkConfiguration,
            LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
            string outputTemplate          = DefaultConsoleOutputTemplate,
            IFormatProvider formatProvider = null,
            LoggingLevelSwitch levelSwitch = null,
            IJSRuntime jsRuntime           = null)
        {
            if (sinkConfiguration == null)
            {
                throw new ArgumentNullException(nameof(sinkConfiguration));
            }
            var formatter = new OutputTemplateRenderer(outputTemplate, formatProvider);

            return(sinkConfiguration.Sink(new BrowserConsoleSink(jsRuntime, formatter), restrictedToMinimumLevel, levelSwitch));
        }
        /// <summary>
        /// Writes log events to <see cref="System.Console"/>.
        /// </summary>
        /// <param name="sinkConfiguration">Logger sink configuration.</param>
        /// <param name="restrictedToMinimumLevel">The minimum level for
        /// events passed through the sink. Ignored when <paramref name="levelSwitch"/> is specified.</param>
        /// <param name="outputTemplate">A message template describing the format used to write to the sink.
        /// The default is <code>"[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}"</code>.</param>
        /// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param>
        /// <param name="levelSwitch">A switch allowing the pass-through minimum level
        /// to be changed at runtime.</param>
        /// <param name="theme">The theme to apply to the styled output. If not specified,
        /// uses <see cref="LINQPadTheme.Literate"/>.</param>
        /// <returns>Configuration object allowing method chaining.</returns>
        public static LoggerConfiguration LINQPad(
            this LoggerSinkConfiguration sinkConfiguration,
            LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
            string outputTemplate          = DefaultConsoleOutputTemplate,
            IFormatProvider formatProvider = null,
            LoggingLevelSwitch levelSwitch = null,
            ConsoleTheme theme             = null)
        {
            if (sinkConfiguration == null)
            {
                throw new ArgumentNullException(nameof(sinkConfiguration));
            }
            if (outputTemplate == null)
            {
                throw new ArgumentNullException(nameof(outputTemplate));
            }

            var appliedTheme = theme ?? DefaultThemes.LINQPadLiterate;

            var formatter = new OutputTemplateRenderer(appliedTheme, outputTemplate, formatProvider);

            return(sinkConfiguration.Sink(new LINQPadSink(appliedTheme, formatter), restrictedToMinimumLevel, levelSwitch));
        }
 public BrowserConsoleSink(IJSRuntime runtime, OutputTemplateRenderer formatter)
 {
     _runtime   = runtime ?? DefaultWebAssemblyJSRuntime.Instance;
     _formatter = formatter ?? throw new ArgumentNullException(nameof(formatter));
 }