代码之家  ›  专栏  ›  技术社区  ›  LP13

.Net Core 1.1在记录时未序列化对象

  •  0
  • LP13  · 技术社区  · 7 年前

    所以如果我直接使用 Serilog.Log 要记录DTO,如下所示

    Serilog.Log.Information("Processing {@DTO}", dto);
    

    但是我正在注射 Microsoft.Extension.Logging.ILogger

            public class HomeController
            {
                private readonly Microsoft.Extensions.Logging.ILogger _logger = null
    
                public HomeController(Microsoft.Extensions.Logging.ILogger logger)
                {
                   _logger = logger;
                }           
    
                public asyc IActionResult Index()
                {               
                    var dto = GetDTO();
    
                    //did not work
                    _logger.LogInformation("DTO {dto}",dto);
    
                    //did not work
                    _logger.LogInformation("DTO {@dto}",dto);
    
                    //worked when i use serilog directly
                    Serilog.Log.Information("DTO {@dto}",dto);
    
                    //did not work
                    Serilog.Log.Information("DTO {dto}",dto);
                }
    
            }
    

    启动。反恐精英

        public Startup(IHostingEnvironment env)
        {
            var builder = new ConfigurationBuilder()
                .SetBasePath(env.ContentRootPath)
                .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
                .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
                .AddEnvironmentVariables();
            Configuration = builder.Build();
    
            if (env.IsDevelopment())
            {
                // This will push telemetry data through Application Insights pipeline faster, allowing you to view results immediately.
                builder.AddApplicationInsightsSettings(developerMode: true);
            }
    
            Configuration = builder.Build();
    
            Log.Logger = new LoggerConfiguration()
            .ReadFrom.Configuration(Configuration)
            .CreateLogger();
    
    }
    

    应用程序设置。json

    "Logging": {
        "IncludeScopes": false,
        "LogLevel": {
          "Default": "Information"
        }
      },
      "Serilog": {
        "WriteTo": [
          {
            "Name": "ColoredConsole"        
          }
        ]
      },
    
    0 回复  |  直到 7 年前
        1
  •  1
  •   LP13    7 年前

    我找到了。我的错误

    Startup.cs Serilog 对于非开发环境,我使用ASPNETCORE_environment=development在本地进行测试。

    if(!env.IsDevelopment())
    {
       loggerFactory.AddSerilog();
    }
    

    我删除了,如果条件和它的工作。 _logger.LogInformation("DTO {@dto}",dto); 作品