Serilog 未记录到 Elasticsearch

2023-12-02

按照此处的 Serilog Elasticsearch 教程进行操作:

https://github.com/serilog/serilog-sinks-elasticsearch#handling-errors

什么都不起作用。我看过其他相关的 SO 帖子,其中有建议+已接受的答案,但没有任何效果。没有错误,控制台和文件接收器工作,但是 ES 永远不会被写入。这是我正在使用的代码:

using System;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Serilog;
using Serilog.Events;
using Serilog.Formatting.Json;
using Serilog.Sinks.Elasticsearch;
using Serilog.Sinks.File;

namespace aspnet.serilog.sample
{
    public class Program
    {
        public static readonly ILogger logger;

        static Program()
        {
            logger = new LoggerConfiguration() // = Log.Logger = new LoggerConfiguration()
               .Enrich.FromLogContext()
               .MinimumLevel.Information()
               .WriteTo.ColoredConsole(
                   LogEventLevel.Information,
                   "{NewLine}{Timestamp:HH:mm:ss} [{Level}] ({CorrelationToken}) {Message}{NewLine}{Exception}")
                .WriteTo.Elasticsearch(new ElasticsearchSinkOptions(new Uri("http://localhost:9200/")){
                    AutoRegisterTemplate = true,
                    AutoRegisterTemplateVersion = AutoRegisterTemplateVersion.ESv7,
                    IndexFormat = "test-index-{0:yyyy.MM.DD}",
                    FailureCallback = e => Console.WriteLine("Unable to submit event " + e.MessageTemplate),
                    EmitEventFailure = EmitEventFailureHandling.WriteToSelfLog |
                                   EmitEventFailureHandling.WriteToFailureSink |
                                   EmitEventFailureHandling.RaiseCallback
                    , MinimumLogEventLevel = LogEventLevel.Information
                })
                .WriteTo.File($"./failures.log", rollingInterval: RollingInterval.Day)
                .CreateLogger();
        }

        public static void Main(string[] args)
        {
            try
            {
                logger.Information($"starting test application....");
                CreateWebHostBuilder(args).Build().Run();
            }
            finally
            {
                logger.Information($"stopping test application.....");
                //Log.CloseAndFlush();
            }
        }

        public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseStartup<Startup>()
                .UseSerilog(logger);
    }
}

我还在 Startup.cs 文件中添加了配置以添加日志记录,但无济于事。


对于那些有同样困难的人,这是我的解决方案:

  1. 从 Startup.cs 文件/类中删除了所有日志记录
  2. 像这样重新配置 Program.cs:
using System;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Serilog;
using Serilog.Events;
using Serilog.Formatting.Json;
using Serilog.Sinks.Elasticsearch;
using Serilog.Sinks.File;

namespace aspnet.serilog.sample
{
    public class Program
    {
        public static readonly ILogger logger;

        static Program()
        {
            logger = new LoggerConfiguration()
                .MinimumLevel.Information()
                .Enrich.FromLogContext()
                .WriteTo.ColoredConsole(LogEventLevel.Information,
                   "{NewLine}{Timestamp:HH:mm:ss} [{Level}] ({CorrelationToken}) {Message}{NewLine}{Exception}")
                .WriteTo.Elasticsearch(new ElasticsearchSinkOptions(
                    new Uri("http://localhost:9200/"))
                    {
                        AutoRegisterTemplate = true,
                        //AutoRegisterTemplateVersion = AutoRegisterTemplateVersion.ESv7,
                        IndexFormat = "test-index-{0:yyyy.MM.dd}",
                        // FailureCallback = e => Console.WriteLine("Unable to submit event " + e.MessageTemplate),
                        // EmitEventFailure = EmitEventFailureHandling.WriteToSelfLog |
                        //                EmitEventFailureHandling.WriteToFailureSink |
                        //                EmitEventFailureHandling.RaiseCallback,
                        //                MinimumLogEventLevel = LogEventLevel.Information,
                        // RegisterTemplateFailure = RegisterTemplateRecovery.IndexAnyway,
                        DeadLetterIndexName = "test-deadletter-{0:yyyy.MM.dd}"
                    })
                .WriteTo.File($"./failures.log", rollingInterval: RollingInterval.Day)
                .CreateLogger();
        }

        public static void Main(string[] args)
        {
            try
            {
                logger.Information($"starting test application....");
                CreateWebHostBuilder(args).Build().Run();
            }
            finally
            {
                logger.Information($"stopping test application.....");
                //Log.CloseAndFlush();
                //((IDisposable)logger)?.Dispose();
            }
        }

        public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseStartup<Startup>()
                .UseSerilog(logger);
    }
}

我留下评论来看看是什么doesn't work.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Serilog 未记录到 Elasticsearch 的相关文章

  • 类的自定义格式

    我当前正在使用 Serilog 我希望能够将一个类传递给记录器并让它在输出到文本文件之前以自定义格式记录 有点类似于格式提供者 https github com serilog serilog wiki Formatting Output
  • 如何在 EF Core 3 中启用日志记录?

    我正在使用 Entity Framework Core 3 Preview 5 和 ASP NET Core 3 Preview 5 在 Visual Studio 2019 的调试输出窗口中 我没有从 EF Core 获取任何日志 我阅读
  • 如何在 ASP.NET 5/vNext/Core 中使用 Elmah?

    我对如何在 ASP NET 5 MVC 6 项目中使用 Elmah 有点困惑 我从 nuget 得到了包 它添加了 Elmah Mvc 2 1 2 到project json 中的依赖项 我不知道从这里到哪里去 以前 nuget 会向 we
  • 如何删除 .net core 中返回 xml 中的 xmlns:xsi 和 xmlns:xsd ?

    这是我的代码 HttpPost Produces application xml public async Task
  • 弹性搜索文档计数

    我正在运行 2 2 版本的 Elastic 搜索 我已经创建了索引并加载了示例文档 我发现其中有些问题 当我给予 GET index type count 我得到了正确的答案 count 9998 shards total 5 succes
  • 如何授权 Swagger 使用 MS Graph API

    我们正在为 MS Graph API 构建 Web API 包装器 我想使用 Swagger 来测试我的 API 但我无法正确配置 我不断收到错误请求 但没有其他线索 我无法在这台公司笔记本电脑上安装 Fiddler 或其他工具来帮助我进行
  • 无法使用docker在Apple Mac芯片M1上启动elasticsearch

    在发布这个问题之前 我浏览了许多链接 例如 Kibana 无法在 Mac M1 上使用 docker 连接到 ElasticSearch https stackoverflow com questions 73160632 kibana c
  • 使用 asp.net core mvc 中的身份验证保护 wwwroot 中的某些文件夹

    我在 wwwroot 中放置了几个包含静态内容的文件夹 对于某些文件夹 我需要用户经过身份验证才能查看这些文件夹 为此 我在启动中添加了代码 如下所示 app UseStaticFiles new StaticFileOptions OnP
  • 如何使用 Serilog ForContext

    我是 Serilog 新手 很难弄清楚如何使用上下文功能 当我运行下面的代码时 输 出文件不包含报告 ID 我缺少什么想法吗 var logger new LoggerConfiguration WriteTo File C Log txt
  • .Net Core appsettings.json 最佳实践 - 覆盖开发设置(或反之亦然)?

    寻找一种关于在 Net Core 中构造 appsettings json 文件的合理方法 是否应该将基本 appsettings json 文件配置为在开发环境中运行 然后基于环境的覆盖 例如 appsettings Production
  • 如何修复从 React 对 Elasticsearch 进行 API 调用时的“混合内容:”错误

    我正在使用 firebase 的 elasticsearch 的 bitnami 版本 我发现它只能连接到http并不是https当我使用邮递员尝试时 我的create react app已部署到 firebase 我得到Mixed Con
  • .NET Core Web API 密钥

    我正在开发一个应用程序 用户可以通过用户名和密码进行身份验证 我们提供一个 JWT 令牌 然后在服务器上进行验证 我想补充的一件事是能够拥有一个特殊的 API 密钥 guid 用户在与此应用程序集成时可以使用该密钥 而不是使用用户名和密码
  • 自定义身份验证中间件 - 如何检查请求是匿名还是授权?

    我正在尝试编写自己的身份验证中间件代码 在老式的 HttpModule 中 当请求 授权 页面时 我可以使用 OnAuthenticateRequest 我的中间件代码是这样的 public async Task Invoke HttpCo
  • MVC 6通过Nuget添加后引用Jquery

    我创建了一个新的 MVC 6 项目 空模板 通过 NuGet 添加了 JQuery 那么如何在 Layout 文件中或您想要使用它的任何地方引用它 我没有包含 Jquery 的脚本文件夹 HERE
  • Elasticsearch 数组必须和must_not

    我的 elasticsearch 数据库中有一个如下所示的文档 tags gt tag 1 tag 2 tag 3 tag A created at gt 2013 07 02 12 42 19 UTC label gt Mon super
  • 通过托管环境进行自托管

    我正在尝试通过以下方式将 ASP NET Core NET 4 6 1 应用程序作为自托管运行网络监听器 https docs asp net en latest fundamentals servers html weblistener
  • Elastic Beanstalk 添加多个 ssl 证书

    我有一个 Docker Django api 应用程序 可以从多个域 abc xyx com 或 def lmn com 调用 我已从 Elastic beanstalk 中的配置控制台成功添加了 abc xyz com 的 ssl 证书
  • MVC6 中的自定义选择性 404 页面

    我正在寻求帮助 我们可以在 ASP NET 5 MVC6 中拥有自定义 Error404 页面 我最接近的是使用 app UseStatusCodePagesWithReExecute Error 0 在 Startup cs 配置方法中
  • NEST 1.0:请参阅 Fiddler 上的请求

    我刚刚更新到 NEST 1 0 我在远程服务器 不是本地主机 上有 Elastic Search 通常我在使用 Fiddler 发送和接收请求时没有任何问题 更新后 bammm 没有检测到任何请求 但我的应用程序发出这些请求没有任何问题 你
  • ASP.NET Core 会话超时

    我记得我们在 ASP NET 中使用了 session timeout 来更改会话超时 如果不更改 则为 20 分钟 我尝试在 Startup cs 中更改 ASP NET Core 3 1 中的会话超时 但没有任何反应 我对操作员使用身份

随机推荐