0

I want to enable EnableSensitiveDataLogging(which is available in Entity Framework Core), but it seems like there is not such a thing in in Entity Framework. is there an alternative to it in Entity Framework? (I'm using EF 6.2, and the reason that I want to enable it is to see the posted data in Serilog's log's file).

 protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
   {
      optionsBuilder.EnableSensitiveDataLogging();
    // ...
   }

EDIT--> I want to log the data model which is posted via my web api using serilog. I'm using Serilog.sinks.File and Swrilog.Sinks.SQLSERVER.

my StartUp.cs class:

    Log.Logger = new LoggerConfiguration()
            .ReadFrom.AppSettings()               
            .WriteTo.MSSqlServer(ConnectionString, 
    "Serilogs").Enrich.WithUserName()
            .Enrich.With<UserNameEnricher> 
   ().Enrich.With<WebApiControllerNameEnricher>()
            .Enrich.WithWebApiRouteData().Enrich
            .WithWebApiControllerName()
            .Enrich.WithWebApiActionName()
            .Enrich.WithWebApiRouteTemplate()               
            .CreateLogger();
        SerilogWebClassic.Configure(cfg => cfg
            .UseLogger(Log.Logger)
            .LogAtLevel(LogEventLevel.Error)
            .EnableFormDataLogging(forms => forms
                .AtLevel(LogEventLevel.Information)
                .OnlyOnError()
                .FilterKeywords(new[] { "password", "authToken" })
            ));
dora
  • 23
  • 4
  • That's not an MVC option in the first place. What are you trying to log? Which EF version are you using? – Panagiotis Kanavos Dec 09 '19 at 12:36
  • ASP.NET MVC is a web app framework, not a data access library. The data access library in this case is Entity Framework Core – Panagiotis Kanavos Dec 09 '19 at 12:38
  • @PanagiotisKanavos I'm using EF 6.2 – dora Dec 09 '19 at 12:41
  • What do you want to log? It's quite likely the data is *already* there and EF Core trimmed it for security reasons. How did you configure Serilog and EF logging? – Panagiotis Kanavos Dec 09 '19 at 12:42
  • [EF Core 3.1](https://www.nuget.org/packages/Microsoft.EntityFrameworkCore/3.1.0) works on all .NET Standard 2.0 runtimes, which means you can use it in .NET Framework too – Panagiotis Kanavos Dec 09 '19 at 12:56
  • you mean this feature I want is not available in EF 6.2? – dora Dec 09 '19 at 13:04
  • No, I mean you can use EF Core 3.1 in older frameworks too. You haven't explained what you want to log yet, or post any code that logs EF 6.2 queries, like the code shown in [Logging and intercepting database operations](https://learn.microsoft.com/en-us/ef/ef6/fundamentals/logging-and-interception). Do you want something more? Or were you looking for EF 6 logging in general? – Panagiotis Kanavos Dec 09 '19 at 13:08
  • I'm using Serilog to log the errors in the catch block of my web api methods. I want Serilog to log the posted data (the data of the posted model ) of the request body in the Serilog Log file. – dora Dec 09 '19 at 13:28
  • did you check the docs that show how to log in EF 6.2? Have you configured the contexts to write to a logger? Without that, you have no logging. – Panagiotis Kanavos Dec 09 '19 at 13:31
  • yes, seems like there's no such thing as enabling the SensitiveDataLogging in EF 6.2 :( – dora Dec 09 '19 at 13:34
  • `Yes` is not an answer to what I asked. In fact, if you *read the docs* you'll see that the sensitive data is *already* logged. `EnableSensitiveDataLogging` doesn't enable logging, it removes the sensitivity filters. *Did you add any logging to the contexts?* – Panagiotis Kanavos Dec 09 '19 at 13:37
  • Check [this possible duplicate](https://stackoverflow.com/questions/16880687/how-can-i-log-the-generated-sql-from-dbcontext-savechanges-in-my-program) and search for the words `Serilog`. You may only need to add `context.Database.Log = Log.Logger.Verbose`, either outside the context, or in its constructor – Panagiotis Kanavos Dec 09 '19 at 13:41

0 Answers0