【问题标题】:Is there a way to stream blazor logs to the client and display them in real time on a web page?有没有办法将 blazor 日志流式传输到客户端并在网页上实时显示它们?
【发布时间】:2020-10-30 19:41:43
【问题描述】:

我试图弄清楚如何配置 Blazor 和 Serilog 以能够呈现将在 razor 页面上显示日志输出的组件。我已经广泛搜索了一个专门使用 Blazor/Asp.Net Core 的示例,但结果很短。

如果我的 Startup 中有以下内容:

   myServiceCollection.AddLogging(l =>
    {
        l.ClearProviders();
        l.AddConsole();
        l.SetMinimumLevel(LogLevel.Debug);
    });

然后在其他一些组件中我注入一个ILogger,而不是登录到控制台,而是将日志重定向到我在相关剃须刀组件上拥有的一些<textarea><p>?我基本上只是想设置一个日志查看器页面或者能够在任意组件上显示日志的输出。

虽然我还没有找到完全符合我要求的具体示例,但有大量 Blazor 聊天应用程序使用 SignalR 在用户之间进行实时聊天的示例。我的想法是我可以重新利用它,而不是在用户之间使用聊天应用程序,我只是在服务器和客户端之间“聊天”,其中消息是输出的日志行。这会是最好的方法吗?

【问题讨论】:

    标签: c# asp.net-core signalr blazor


    【解决方案1】:

    您可以创建自定义记录器。我设法让它与这个 tutorial chat-app

    一起工作

    Startup.cs

    loggerFactory.AddProvider(
        new SignalRLoggerProvider(
            new SignalRLoggerConfiguration
            {
                HubContext = serviceProvider.GetService<IHubContext<BlazorChatSampleHub>>(),
                LogLevel = LogLevel.Information
            }));
    
    
    
    public class SignalRLoggerProvider : ILoggerProvider
    {
        private readonly SignalRLoggerConfiguration _config;
        private readonly ConcurrentDictionary<string, SignalRLogger> _loggers = new ConcurrentDictionary<string, SignalRLogger>();
    
        public SignalRLoggerProvider(SignalRLoggerConfiguration config)
        {
            _config = config;
        }
    
        public ILogger CreateLogger(string categoryName)
        {
            return _loggers.GetOrAdd(categoryName, name => new SignalRLogger(name, _config));
        }
    
        public void Dispose()
        {
            _loggers.Clear();
        }
    }
    
    
    public class SignalRLogger : ILogger
    {
        private readonly string _name;
        private readonly SignalRLoggerConfiguration _config;
        public SignalRLogger(string name, SignalRLoggerConfiguration config)
        {
            _name = name;
            _config = config;
        }
    
        public IDisposable BeginScope<TState>(TState state)
        {
            return null;
        }
    
        public bool IsEnabled(LogLevel logLevel)
        {
            return logLevel == _config.LogLevel;
        }
    
        public void Log<TState>(LogLevel logLevel, EventId eventId, TState state,
                            Exception exception, Func<TState, Exception, string> formatter)
        {
            if (!IsEnabled(logLevel))
            {
                return;
            }
    
            if (_config.EventId == 0 || _config.EventId == eventId.Id)
            {
                this._config.HubContext?.Clients.Group(_config.GroupName).SendAsync("Broadcast", "LOGGER", $"{DateTimeOffset.UtcNow:T}-UTC : {formatter(state, exception)}");
            }
        }
    }
    
    

    Proof of concept

    【讨论】:

    猜你喜欢
    • 2018-02-09
    • 1970-01-01
    • 1970-01-01
    • 2021-08-31
    • 2019-11-28
    • 1970-01-01
    • 2022-01-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多