8

I use nlog and following setting to log sql queries:

<logger name="Microsoft.EntityFrameworkCore.*" 
        minlevel="Trace" writeTo="sqllogfile" final="true" />

It works as expected, but doesn't log parameter values, the queries look like this:

2017-07-31 13:49:03.8836|  INFO  |Microsoft.EntityFrameworkCore.Internal.InterceptingLogger`1.Log|Executed DbCommand (8ms) [Parameters=[@__get_Item_0='?' (Size = 450)], CommandType='Text', CommandTimeout='30']
SELECT TOP(1) [e].[Id], [e].[AccessFailedCount], [e].[ConcurrencyStamp], [e].[Email], [e].[EmailConfirmed], [e].[HeliosLoginId], [e].[LockoutEnabled], [e].[LockoutEnd], [e].[Name], [e].[NormalizedEmail], [e].[NormalizedUserName], [e].[PasswordHash], [e].[PhoneNumber], [e].[PhoneNumberConfirmed], [e].[SecurityStamp], [e].[TwoFactorEnabled], [e].[UserName]
FROM [AspNetUsers] AS [e]
WHERE [e].[Id] = @__get_Item_0 

Is it possible to show value of the @__get_Item_0 parameter ?

Thank you

2
  • 1
    On your DbContextOptionsBuilder call EnableSensitiveDataLogging By default EF Core does not log values since values may contain sensitive data. Commented Jul 31, 2017 at 23:34
  • @Smit I consider this an answer, feel free to post it again. Thank you Commented Aug 1, 2017 at 5:08

1 Answer 1

20

By default, EF Core hides the data in the SQL which gets logged. Queries can have various data some of which can be sensitive information (like customer's social security number or credit card information). Therefore logs have ? instead of actual values.

Though at times, developer may want to see the values especially while debugging nasty bugs. To enable logging actual values, you need to configure your dbcontext.

You need to call EnableSensitiveDataLogging() on your DbContextOptionsBuilder. Since there are multiple ways to configure db context options, easiest way to get hold of it would be where you are configuring your provider with connection string (e.g. UseSqlServer) you can chain it right after it.

Example

optionsBuilder
    .UseSqlServer("connectionstring")
    .EnableSensitiveDataLogging();
Sign up to request clarification or add additional context in comments.

1 Comment

Is there a similar configuration option for EF6?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.