[EF Core] How to Enable Sensitive Data Logging and Detailed Errors


Question: How do you enable sensitive data and detailed error #logging in the #Development Environment for debugging purposes? EF Core does not show detailed errors. 


Login to See the Rest of the Answer

Answer:
When programming, a Software Developer might need to have visibility to detailed Error Messages in order to resolve Software Bugs if any. If this is the case, keep in mind that Sensitive data logging is not advisable in production along with Detailed Error logging. Enable these flags at the risk of bad actors having access to the logs in Production.


See the code below on how to enable Detailed Error and Sensitive Data logging in Asp.net Core and Entity Framework Core.

 services.AddDbContext<ApplicationDbContext>(options =>
       options.UseSqlServer(
             Configuration.GetConnectionString("dbConnection"),
                    sqlServerOptionsAction: sqlOptions =>
                    {
                        //Solves the issue of dropping the connection
                        // to the DB
                        sqlOptions.EnableRetryOnFailure(
                        maxRetryCount: 20,
                        maxRetryDelay: TimeSpan.FromSeconds(38),
                        errorNumbersToAdd: null);
                    }
          ), ServiceLifetime.Transient).EnableSensitiveDataLogging()
           .EnableDetailedErrors()
        );


- When injecting a Database Context into a Dependency Injection Container, it is safe to inject it as a Transient and not a Scopped Service.






© 2024 - ErnesTech - Privacy
E-Commerce Return Policy