• Home
  • About
  • Contact
  • Privacy
  • Terms
  • DCMA
  • Write For Us / Submit
Tech News, Magazine & Review WordPress Theme 2017
  • Tech
    transfer whatsapp from android to iphone

    How to Transfer WhatsApp from Android to iPhone!

    Understanding the Role of Humidity Chambers in Climate Testing

    Understanding the Role of Humidity Chambers in Climate Testing

    Everything You Get to Know About Movember Beard Memes

    Everything You Get to Know About Movember Beard Memes

    Best Book Recommendation Apps

    11 Best Book Recommendation Apps

    How to quickly divide or Split PDF files

    How to Quickly Divide or Split PDF Files

    Enhance your omnichannel service strategies with data-driven insights.

    Black Market for Fraudulent Ad Accounts Found on Facebook

    Sen. Mark Warner and Sen. Marco Rubio at a hearing.

    Trump’s TikTok ban might have been right after all

    14 Gift Ideas for the Home Chef (2022): Vitamix, Cookbooks, and More

    14 Gift Ideas for the Home Chef (2022): Vitamix, Cookbooks, and More

    6 Android Apps for Budget Management

    Twitter, Facebook, Lyft layoffs spark fears of dotcom crash 2.0

  • Gear
    • All
    • Camera
    • Laptop
    • Smartphone
    Latest Smart Home Gadgets for a Connected Life

    Latest Smart Home Gadgets for a Connected Life

    Eco-Friendly Products for Students

    Eco-Friendly Products for Students

    Essential Photography Equipment and Gadgets

    Mastering Your Shots: Essential Photography Equipment and Gadgets

    Level Up Your Game: Must-Have Gaming Gear!

    Level Up Your Game: Must-Have Gaming Gear!

    Pixel Watch in the test

    Google Pixel Watch in the Test

    Top 5 Websites To Buy Camera Lenses At Reasonable Price

    Top 5 Websites To Buy Camera Lenses At Reasonable Price

    Trending Tags

    • Best iPhone 7 deals
    • Apple Watch 2
    • Nintendo Switch
    • CES 2017
    • Playstation 4 Pro
    • iOS 10
    • iPhone 7
    • Sillicon Valley
  • Gaming
    9 Best Alternative Games Like Kahoot

    9 Best Alternative Games Like Kahoot

    The Top 8 Free Bubble Shooter Games for Endless Entertainment

    The Top 8 Free Bubble Shooter Games for Endless Entertainment

    Cloud Gaming Revolution: How Streaming is Changing the Future of Gaming

    Cloud Gaming Revolution: How Streaming is Changing the Future of Gaming

    Level Up Your Game: Must-Have Gaming Gear!

    Level Up Your Game: Must-Have Gaming Gear!

    Game Streaming Platforms: Evolution & Impact on Industry

    Game Streaming Platforms: Evolution & Impact on Industry

    The Rise of Esports Games

    Esports Games: Exploring the Ecosystem, Growth, and Impact

  • Crypto
    transfer whatsapp from android to iphone

    How to Transfer WhatsApp from Android to iPhone!

    Understanding the Role of Humidity Chambers in Climate Testing

    Understanding the Role of Humidity Chambers in Climate Testing

    Everything You Get to Know About Movember Beard Memes

    Everything You Get to Know About Movember Beard Memes

    Best Book Recommendation Apps

    11 Best Book Recommendation Apps

    How to quickly divide or Split PDF files

    How to Quickly Divide or Split PDF Files

    Enhance your omnichannel service strategies with data-driven insights.

    Black Market for Fraudulent Ad Accounts Found on Facebook

    Sen. Mark Warner and Sen. Marco Rubio at a hearing.

    Trump’s TikTok ban might have been right after all

    14 Gift Ideas for the Home Chef (2022): Vitamix, Cookbooks, and More

    14 Gift Ideas for the Home Chef (2022): Vitamix, Cookbooks, and More

    6 Android Apps for Budget Management

    Twitter, Facebook, Lyft layoffs spark fears of dotcom crash 2.0

  • Business
    transfer whatsapp from android to iphone

    How to Transfer WhatsApp from Android to iPhone!

    Understanding the Role of Humidity Chambers in Climate Testing

    Understanding the Role of Humidity Chambers in Climate Testing

    Everything You Get to Know About Movember Beard Memes

    Everything You Get to Know About Movember Beard Memes

    Best Book Recommendation Apps

    11 Best Book Recommendation Apps

    How to quickly divide or Split PDF files

    How to Quickly Divide or Split PDF Files

    Enhance your omnichannel service strategies with data-driven insights.

    Black Market for Fraudulent Ad Accounts Found on Facebook

    Sen. Mark Warner and Sen. Marco Rubio at a hearing.

    Trump’s TikTok ban might have been right after all

    14 Gift Ideas for the Home Chef (2022): Vitamix, Cookbooks, and More

    14 Gift Ideas for the Home Chef (2022): Vitamix, Cookbooks, and More

    6 Android Apps for Budget Management

    Twitter, Facebook, Lyft layoffs spark fears of dotcom crash 2.0

No Result
View All Result
Geeky Insider
  • Tech
    transfer whatsapp from android to iphone

    How to Transfer WhatsApp from Android to iPhone!

    Understanding the Role of Humidity Chambers in Climate Testing

    Understanding the Role of Humidity Chambers in Climate Testing

    Everything You Get to Know About Movember Beard Memes

    Everything You Get to Know About Movember Beard Memes

    Best Book Recommendation Apps

    11 Best Book Recommendation Apps

    How to quickly divide or Split PDF files

    How to Quickly Divide or Split PDF Files

    Enhance your omnichannel service strategies with data-driven insights.

    Black Market for Fraudulent Ad Accounts Found on Facebook

    Sen. Mark Warner and Sen. Marco Rubio at a hearing.

    Trump’s TikTok ban might have been right after all

    14 Gift Ideas for the Home Chef (2022): Vitamix, Cookbooks, and More

    14 Gift Ideas for the Home Chef (2022): Vitamix, Cookbooks, and More

    6 Android Apps for Budget Management

    Twitter, Facebook, Lyft layoffs spark fears of dotcom crash 2.0

  • Gear
    • All
    • Camera
    • Laptop
    • Smartphone
    Latest Smart Home Gadgets for a Connected Life

    Latest Smart Home Gadgets for a Connected Life

    Eco-Friendly Products for Students

    Eco-Friendly Products for Students

    Essential Photography Equipment and Gadgets

    Mastering Your Shots: Essential Photography Equipment and Gadgets

    Level Up Your Game: Must-Have Gaming Gear!

    Level Up Your Game: Must-Have Gaming Gear!

    Pixel Watch in the test

    Google Pixel Watch in the Test

    Top 5 Websites To Buy Camera Lenses At Reasonable Price

    Top 5 Websites To Buy Camera Lenses At Reasonable Price

    Trending Tags

    • Best iPhone 7 deals
    • Apple Watch 2
    • Nintendo Switch
    • CES 2017
    • Playstation 4 Pro
    • iOS 10
    • iPhone 7
    • Sillicon Valley
  • Gaming
    9 Best Alternative Games Like Kahoot

    9 Best Alternative Games Like Kahoot

    The Top 8 Free Bubble Shooter Games for Endless Entertainment

    The Top 8 Free Bubble Shooter Games for Endless Entertainment

    Cloud Gaming Revolution: How Streaming is Changing the Future of Gaming

    Cloud Gaming Revolution: How Streaming is Changing the Future of Gaming

    Level Up Your Game: Must-Have Gaming Gear!

    Level Up Your Game: Must-Have Gaming Gear!

    Game Streaming Platforms: Evolution & Impact on Industry

    Game Streaming Platforms: Evolution & Impact on Industry

    The Rise of Esports Games

    Esports Games: Exploring the Ecosystem, Growth, and Impact

  • Crypto
    transfer whatsapp from android to iphone

    How to Transfer WhatsApp from Android to iPhone!

    Understanding the Role of Humidity Chambers in Climate Testing

    Understanding the Role of Humidity Chambers in Climate Testing

    Everything You Get to Know About Movember Beard Memes

    Everything You Get to Know About Movember Beard Memes

    Best Book Recommendation Apps

    11 Best Book Recommendation Apps

    How to quickly divide or Split PDF files

    How to Quickly Divide or Split PDF Files

    Enhance your omnichannel service strategies with data-driven insights.

    Black Market for Fraudulent Ad Accounts Found on Facebook

    Sen. Mark Warner and Sen. Marco Rubio at a hearing.

    Trump’s TikTok ban might have been right after all

    14 Gift Ideas for the Home Chef (2022): Vitamix, Cookbooks, and More

    14 Gift Ideas for the Home Chef (2022): Vitamix, Cookbooks, and More

    6 Android Apps for Budget Management

    Twitter, Facebook, Lyft layoffs spark fears of dotcom crash 2.0

  • Business
    transfer whatsapp from android to iphone

    How to Transfer WhatsApp from Android to iPhone!

    Understanding the Role of Humidity Chambers in Climate Testing

    Understanding the Role of Humidity Chambers in Climate Testing

    Everything You Get to Know About Movember Beard Memes

    Everything You Get to Know About Movember Beard Memes

    Best Book Recommendation Apps

    11 Best Book Recommendation Apps

    How to quickly divide or Split PDF files

    How to Quickly Divide or Split PDF Files

    Enhance your omnichannel service strategies with data-driven insights.

    Black Market for Fraudulent Ad Accounts Found on Facebook

    Sen. Mark Warner and Sen. Marco Rubio at a hearing.

    Trump’s TikTok ban might have been right after all

    14 Gift Ideas for the Home Chef (2022): Vitamix, Cookbooks, and More

    14 Gift Ideas for the Home Chef (2022): Vitamix, Cookbooks, and More

    6 Android Apps for Budget Management

    Twitter, Facebook, Lyft layoffs spark fears of dotcom crash 2.0

Submit
Geeky Insider
No Result
View All Result

How to work with logging in EF Core 7

by Staff Writer
November 10, 2022
in How To
Reading Time: 4 mins read
logs
Share on FacebookShare on Twitter

Entity Framework Core (EF Core) is a modern, open-source, object-database mapper that simplifies data access for .NET applications. EF Core enables you to work with data from a variety of sources including relational databases, non-relational databases, and even in-memory data.

EF Core allows you to write code to execute CRUD actions (create, read, update, and delete) without understanding how the data is persisted in the underlying database. Using EF Core, you can retrieve entities from a data store, add or change or delete entities, and traverse entity graphs.

Table of Contents

    • READ ALSO
    • How to Turn Off Autoplay on YouTube
    • How to go through your CV in a job interview
  • Create an ASP.NET Core minimal Web API project in Visual Studio 2022 Preview
  • Logging options in Entity Framework Core
  • Create a Customer class in .NET Core
  • Configure logging in Entity Framework Core
  • Send EF Core log data to the console
  • Complete source code of our DbContext class
  • Flexible logging with EF Core

READ ALSO

How to protect yourself from phishing

Phishing: The nasty tricks of the cybercriminals – this is how they protect themselves

July 28, 2023
How to Unsend an Email in Outlook

How to Unsend an Email in Outlook

November 16, 2022

In other words, EF Core simplifies your data access by allowing you to write code that performs those CRUD operations using .NET objects, without directly interacting with the underlying database provider. This article talks about how you can use Entity Framework Core to log database activity when working in ASP.NET Core 7 applications.

To work with the code examples provided in this article, you should have Visual Studio 2022 Preview installed in your system. If you don’t already have a copy, you can download Visual Studio 2022 Preview here.

Create an ASP.NET Core minimal Web API project in Visual Studio 2022 Preview

First off, let’s create an ASP.NET Core project in Visual Studio 2022. Following these steps will create a new ASP.NET Core Web API 6 project in Visual Studio 2022:

  1. Launch the Visual Studio 2022 Preview IDE.
  2. Click on “Create new project.”
  3. In the “Create new project” window, select “ASP.NET Core Web API” from the list of templates displayed.
  4. Click Next.
  5. In the “Configure your new project” window, specify the name and location for the new project.
  6. Optionally check the “Place solution and project in the same directory” check box, depending on your preferences.
  7. Click Next.
  8. In the “Additional Information” window shown next, select .NET 7.0 (Preview) as the framework version you want to use.
  9. Next, uncheck the check box that says “Use controllers…” since we’ll be using minimal APIs in this example. Leave the “Authentication Type” set as “None” (default).
  10. Ensure that the check boxes “Enable Docker,” “Configure for HTTPS,” and “Enable Open API Support” are unchecked as we won’t be using any of those features here.
  11. Click Create.

This will create a new ASP.NET Core 7 Web API project in Visual Studio 2022. We’ll use this project to illustrate logging in EF Core 7 in the subsequent sections of this article.

Logging options in Entity Framework Core

In Entity Framework Core, logging is used to track database queries and other operations. Entity Framework Core uses the Microsoft.Extensions.Logging framework to log events. This framework provides a wide variety of logging providers that can be used to record log messages.

By default, Entity Framework Core will write log messages to the console. In addition to using the Microsoft.Extensions.Logging framework, Entity Framework Core also supports third-party logging frameworks such as NLog and Serilog. These frameworks can be used to write log messages to files, databases, or other destinations.

If you intend to use EF Core and SQL Server in your application, you will need to add the Microsoft.EntityFrameworkCore.SqlServer NuGet package to your project. Select the project in Solution Explorer, then right-click and select “Manage NuGet Packages.” In the NuGet Package Manager, search for Microsoft.EntityFrameworkCore.SqlServer and install the package.

Alternatively, you can install the package via the NuGet Package Manager console by entering the command shown below.

PM> Install-Package Microsoft.EntityFrameworkCore.SqlServer

Create a Customer class in .NET Core

Create a class named Customer in a file having the same name with a “.cs” extension and write the following code in there:

 
public class Customer
{
public int Id { get; set; }
public string FirstName { get; set; } = string.Empty;
public string LastName { get; set; } = string.Empty;
}

Configure logging in Entity Framework Core

You can take advantage of logging in Entity Framework Core in either of two ways:

  • Use the UseLoggerFactory extension method.
  • Use the ILoggerProvider interface.

The UseLoggerFactory extension method is the recommended way to configure logging in EF Core, as it allows for a more flexible configuration. To use the UseLoggerFactory extension method, simply add it to your DbContext class as shown in the code snippet given below.

 
using Microsoft.Extensions.Logging;
public class MyDbContext : DbContext
{
public MyDbContext(DbContextOptions options)
: base(options){ }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseLoggerFactory(loggerFactory);
base.OnConfiguring(optionsBuilder);
}
ILoggerFactory loggerFactory = new LoggerFactory();
}

Alternatively, you can configure logging by using the ILoggerProvider interface as shown in the code snippet given below.

builder.Services.AddDbContext((provider, options) =>
{
var loggerFactory = provider.GetRequiredService();
options.UseLoggerFactory(loggerFactory);
});

Send EF Core log data to the console

You can use the following code to configure your DbContext instance to use  LoggerFactory.

optionsBuilder.UseLoggerFactory(loggerFactory)
.EnableSensitiveDataLogging()
.UseSqlServer(“Server=JOYDIP;Database=DemoDb;Trusted_Connection=True;”);

To enable EF Core to log data to the console, you can use the following code.

optionsBuilder.UseSqlServer
(“Server=JOYDIP;Database=DemoDb;Trusted_Connection=True;”).
LogTo(Console.WriteLine).EnableDetailedErrors();

Complete source code of our DbContext class

The complete source code of the DbContext class is given below for your reference.

public partial class DemoDbContext: DbContext
{
public DemoDbContext()
{}
public DemoDbContext(DbContextOptions < DemoDbContext > options): base(options)
{}
public virtual DbSet < Customer > Customer
{
get;
set;
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(“Server=JOYDIP;Database=DemoDb;Trusted_Connection=True;”).LogTo(Console.WriteLine).EnableDetailedErrors();
base.OnConfiguring(optionsBuilder);
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity < Customer > (entity =>
{
entity.Property(e => e.FirstName).IsRequired().HasMaxLength(50);
entity.Property(e => e.LastName).IsRequired().HasMaxLength(50);
});
}
}
 

Flexible logging with EF Core

Logging is a crucial component of any application for identifying and analyzing problems that can occur at runtime. When working with EF Core, you can log data to built-in log targets as well as integrate your application with third-party logging frameworks to send data to preconfigured log targets.

Copyright © 2022 IDG Communications, Inc.

Source by www.infoworld.com

Related Posts

transfer whatsapp from android to iphone
Articles

How to Transfer WhatsApp from Android to iPhone!

September 19, 2023
How to prevent phishing attacks
How To

How to Prevent Phishing Attacks: Safeguarding Your Digital Identity

May 14, 2023
How to Activate Iphone With Sim Card
Apple

How to Activate Iphone With Sim Card

March 3, 2023
How to quickly divide or Split PDF files
Apps And Softwares

How to Quickly Divide or Split PDF Files

February 28, 2023

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recommended.

NYCC 2022: One Piece Film Red Invades New York Comic Con

NYCC 2022: One Piece Film Red Invades New York Comic Con

October 12, 2022
6 Android Apps for Budget Management

Pixel 7 Confirmed to Run New In-House Processor

September 7, 2022

Trending.

MicroSD card in hand.

How to Move Apps to SD Card on an Amazon Fire Tablet

October 5, 2022
Google's No Internet Dino game running on a laptop computer

How to Hack the Hidden Google Chrome Dinosaur Game

October 23, 2022
Employee Payroll Management Software Simplifying Processes

Employee Payroll Management Software: Simplifying Processes

August 23, 2023
Symbolic Link Header Image. A Windows folder on a blue background.

The Complete Guide to Creating Symbolic Links (aka Symlinks) on Windows

July 27, 2022
Docker logo

How to Run Docker Compose Containers With GPU Access

July 24, 2022
Geeky Insider

Geeky Insider is Web based independent Tech Magazine covering Topics Related to Technology, Gadgets, Software, Gaming, Crypto, Business And So on. Read More...

Submit a Techy Post | Write For Us

Feel free to contact us for submission queries. via contact form or email us at [email protected].

Categories

  • Android
  • Apple
  • Apps And Softwares
  • Articles
  • Business
  • Camera
  • Crypto
  • Education
  • Entertainment
  • Gadget and Gear
  • Gaming
  • How To
  • Laptop
  • Marketing
  • Microsoft
  • Review
  • Security
  • Smartphone
  • Technology
00-samsung FRP unlock tool

Unlock Samsung in Minutes: 5 Proven Hacks with the Ultimate FRP Unlock Tool!

September 19, 2023
transfer whatsapp from android to iphone

How to Transfer WhatsApp from Android to iPhone!

September 19, 2023
Understanding the Role of Humidity Chambers in Climate Testing

Understanding the Role of Humidity Chambers in Climate Testing

September 4, 2023
  • Home
  • About
  • Contact
  • Privacy
  • Terms
  • DCMA
  • Write For Us / Submit

© 2022 GeekyInsider.com

No Result
View All Result
  • Home
  • Review
  • Apple
  • Gaming
  • Gadget and Gear
    • Camera
    • Smartphone
  • Microsoft
  • Security