Guozhen AIGlobal AI field notes and model intelligence

English translation

Logging Configuration in ASP.NET Core Zero

Published:

Category: ASP.NET

Read time: 2 min

Reads: 0

Lesson #26Views are counted together with the original Chinese articleImages are preserved from the source page

AI Article Decision Snapshot

Turn the lesson into workflow, model, budget, and security checks before choosing tools.

Use this quick snapshot before leaving the article. It keeps the next search tied to practical AI software, model/API, cost, privacy, and implementation questions.

Workflow fit

Identify the real job behind the article: coding, research, document review, support, analytics, content, or internal automation.

Model or tool decision

Decide whether the next step is a software shortlist, an AI tool comparison, an API platform choice, or a model benchmark.

Budget and usage signal

Estimate seats, API calls, prompt volume, retries, review time, and fallback work before assuming the workflow is cheap.

Security and privacy review

Check whether source code, customer data, private documents, prompts, logs, or embeddings will enter the AI workflow.

In the previous article, we discussed the fundamental concepts of logging—including its purpose, categories of logs, and common log levels. In this part, we’ll delve into how to configure logging providers in ASP.NET Core to ensure your application can flexibly and effectively record the required log information.

What Is a Logging Provider?

A logging provider is a concrete implementation responsible for writing log entries to specific destinations—such as the console, files, databases, or external services. ASP.NET Core ships with several built-in logging providers and also supports custom logging providers developed by third parties or your own team.

Configuring Logging Providers

In ASP.NET Core, logging providers are typically configured in the Program.cs file. By default, newly created ASP.NET Core projects already include basic logging providers—namely, the Console and Debug providers. Below, we walk through how to configure and add additional logging providers.

1. Default Configuration

Here’s an example of a minimal Program.cs file for a new ASP.NET Core application, which already includes console and debug logging:

using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;

public class Program
{
    public static void Main(string[] args)
    {
        CreateHostBuilder(args).Build().Run();
    }

    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup<Startup>();
            });
}

In this example, ASP.NET Core uses the CreateDefaultBuilder method to automatically register the Console and Debug logging providers.

2. Adding a File-Based Logging Provider

To persist log entries to disk, you can integrate libraries such as Serilog. The following steps demonstrate how to add file-based logging using Serilog.

2.1 Install Required NuGet Packages

Add the following NuGet packages to your project:

dotnet add package Serilog.AspNetCore
dotnet add package Serilog.Extensions.Logging
dotnet add package Serilog.Sinks.File

2.2 Configure Serilog

Next, configure Serilog in Program.cs:

using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
using Serilog;

public class Program
{
    public static void Main(string[] args)
    {
        Log.Logger = new LoggerConfiguration()
            .MinimumLevel.Information()
            .WriteTo.Console()
            .WriteTo.File("logs/log-.txt", rollingInterval: RollingInterval.Day)
            .CreateLogger();

        try
        {
            Log.Information("Application Starting");
            CreateHostBuilder(args).Build().Run();
        }
        catch (Exception ex)
        {
            Log.Fatal(ex, "Application Start-up Failed");
        }
        finally
        {
            Log.CloseAndFlush();
        }
    }

    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .UseSerilog() // Enable Serilog
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup<Startup>();
            });
}

Here, Serilog is configured with a minimum log level of Information, and output is directed to both the console and a rotating file. A new log file is generated daily, named in the format log-YYYYMMDD.txt.

3. Configuring Log Levels

You can dynamically adjust verbosity across environments by configuring log levels in appsettings.json. For example:

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  }
}

In this configuration:

  • The default log level is set to Information.
  • All log entries under the Microsoft namespace (e.g., framework internals) are suppressed below Warning.
  • Microsoft.Hosting.Lifetime messages remain at Information, ensuring startup/shutdown events are visible.

Summary

In this article, we explored how to configure logging providers in ASP.NET Core—including integrating Serilog for file-based logging. Thoughtful configuration of logging providers enables flexible, environment-aware log management—helping you diagnose issues quickly and monitor application health effectively.

In the next article, we’ll cover how to write log entries, and how to leverage different log levels to capture meaningful, actionable insights. Stay tuned!

Apply This Lesson

Turn this article into AI software, model, API, and security decisions.

English Article FAQ

Use this article as evidence before choosing AI tools

How should I use this AI Tutorials article?

Use it as the implementation or learning layer, then connect the idea to AI software buyer guides, tool comparisons, benchmarks, API choices, and security checks before making a production decision.

Is this English article different from the Chinese original?

The English edition is localized for global AI readers while preserving the original diagrams, screenshots, prompts, code examples, and source context from the Chinese article.

What should I read after Logging Configuration in ASP.NET Core Zero?

Continue with AI Software Buyer Guides, AI Tools Workbench, Best AI Coding Agents, AI Model Benchmarks, OpenAI vs Anthropic API, or LLM Security Tools depending on the decision you need to make.

Can this article alone choose an AI product or model?

No. Treat the article as evidence and context, then validate fit with pricing, privacy requirements, integration effort, benchmark results, workflow tests, and fallback planning.

Continue

Keep reading from here

Browse English site

Reader Messages

Reader messages

Questions, corrections, extra sources, or hands-on results can be left here. No login is required.

Max 800 characters

To reduce spam, each message is checked for length, link count, and posting frequency.

0/800

Messages

0 messages
Loading messages...