Skip to main content
Login Register
Code2night
  • Home
  • Blog Archive
  • Learn
    • Tutorials
    • Videos
  • Interview Q&A
  • Products
  • Resources
    • Cheatsheets
    • Tech Comparisons
  • Languages
    • Angular Angular js ASP.NET Asp.net Core ASP.NET Core, C# ASP.NET MVC ASP.NET Web Forms C C# C#, ASP.NET Core, Dapper
      C#, ASP.NET Core, Dapper, Entity Framework DotNet General Web Development HTML, CSS HTML/CSS Java JavaScript JavaScript, HTML, CSS JavaScript, Node.js Node.js
      Python Python 3.11, Pandas, SQL Python 3.11, SQL Python 3.11, SQLAlchemy Python 3.11, SQLAlchemy, SQL Python 3.11, SQLite React Security SQL Server TypeScript
  • Post Blog
  • Tools
    • Beautifiers
      JSON Beautifier HTML Beautifier XML Beautifier CSS Beautifier JS Beautifier SQL Formatter
      Dev Utilities
      JWT Decoder Regex Tester Diff Checker Cron Explainer String Escape Hash Generator Password Generator
      Converters
      Base64 Encode/Decode URL Encoder/Decoder JSON to CSV CSV to JSON JSON to TypeScript Markdown to HTML Number Base Converter Timestamp Converter Case Converter
      Generators
      UUID / GUID Generator Lorem Ipsum QR Code Generator Meta Tag Generator
      Image Tools
      Image Converter Image Resizer Image Compressor Image to Base64 PNG to ICO Background Remover Color Picker
      Text & Content
      Word Counter PDF Editor
      SEO & Web
      SEO Analyzer URL Checker World Clock
  1. Home
  2. Blog
  3. ASP.NET Core
  4. CWE-778: Implementing Security Audit Logging in ASP.NET Core with Serilog and Seq

CWE-778: Implementing Security Audit Logging in ASP.NET Core with Serilog and Seq

Date- Jun 07,2026 260

Overview

The CWE-778 designation refers to the 'Insufficient Logging' vulnerability, which arises when applications fail to adequately log security-relevant events. This oversight can lead to undetected attacks or unauthorized access, making it challenging to perform forensic analysis and respond to incidents effectively. By implementing a robust security audit logging framework, developers can gain visibility into application behavior and user actions, thereby enhancing the overall security posture.

In ASP.NET Core applications, logging is a first-class citizen, allowing developers to capture detailed information about application execution. This article focuses on using Serilog, a powerful logging library, in conjunction with Seq, a structured log server, to implement security audit logging. Real-world use cases include tracking user authentication attempts, monitoring access to sensitive data, and logging administrative actions.

Prerequisites

  • ASP.NET Core SDK: Ensure you have the latest version of the ASP.NET Core SDK installed on your machine.
  • Serilog: Familiarity with Serilog, which provides advanced logging capabilities for .NET applications.
  • Seq: An understanding of Seq, which is used to store and query structured logs.
  • NuGet: Basic knowledge of using NuGet packages to add dependencies to your ASP.NET Core project.

Setting Up Serilog in ASP.NET Core

To begin implementing audit logging, we first need to set up Serilog within our ASP.NET Core application. Serilog allows us to log events in a structured format, which is essential for later querying and analysis in Seq.

public class Program
{
public static void Main(string[] args)
{
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.WriteTo.Console()
.WriteTo.Seq("http://localhost:5341")
.CreateLogger();

try
{
Log.Information("Starting up the application...");
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() // Integrate Serilog
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}

This code sets up a Serilog logger that outputs logs to both the console and Seq. The LoggerConfiguration method allows for fine-tuning the logging levels and destinations.

Line-by-line breakdown:

  • Log.Logger = new LoggerConfiguration(): Initializes a new logger configuration.
  • .MinimumLevel.Debug(): Sets the minimum log level to Debug, which captures all logs.
  • .WriteTo.Console(): Configures the logger to write log messages to the console.
  • .WriteTo.Seq("http://localhost:5341"): Directs logs to the Seq server running locally.
  • CreateLogger(): Completes the logger configuration and creates the logger instance.

Once the logger is configured, you can run your application and check the console and Seq for log output.

Configuring Seq

To set up Seq, download and install it from the official Seq website. Once installed, run the application, and it will listen for incoming log events on the specified URL. You can access the Seq dashboard through a web browser at http://localhost:5341.

Implementing Security Audit Logging

With Serilog configured, the next step is to implement security audit logging. This typically involves logging events such as user logins, failed login attempts, and access to sensitive resources.

public class AccountController : Controller
{
private readonly ILogger<AccountController> _logger;

public AccountController(ILogger<AccountController> logger)
{
_logger = logger;
}

[HttpPost]
public IActionResult Login(LoginViewModel model)
{
if (ModelState.IsValid)
{
// Simulate user authentication
bool isAuthenticated = AuthenticateUser(model.Username, model.Password);

if (isAuthenticated)
{
_logger.LogInformation("User {Username} logged in successfully", model.Username);
return RedirectToAction("Index", "Home");
}
else
{
_logger.LogWarning("Failed login attempt for user {Username}", model.Username);
return View();
}
}
return View();
}
}

This code snippet demonstrates a login action method within an AccountController. The logger captures both successful and failed login attempts.

Line-by-line breakdown:

  • ILogger<AccountController> _logger: Declares a logger instance for the controller.
  • _logger.LogInformation(...): Logs a success message when a user logs in.
  • _logger.LogWarning(...): Logs a warning when a login attempt fails.

In this way, security audit logs can be generated for critical user actions, which can later be analyzed for suspicious activities.

Logging Sensitive Data

When logging security events, be mindful of sensitive data. Logging sensitive information such as passwords or personal identification numbers can lead to security breaches. Serilog provides options to filter out sensitive data before logging.

public class SensitiveDataProtectionMiddleware
{
private readonly RequestDelegate _next;

public SensitiveDataProtectionMiddleware(RequestDelegate next)
{
_next = next;
}

public async Task Invoke(HttpContext context)
{
var originalRequestBody = context.Request.Body;
using (var newRequestBody = new MemoryStream())
{
await context.Request.Body.CopyToAsync(newRequestBody);
newRequestBody.Seek(0, SeekOrigin.Begin);

var requestBodyText = await new StreamReader(newRequestBody).ReadToEndAsync();
// Filter out sensitive data before logging
requestBodyText = FilterSensitiveData(requestBodyText);
Log.Information("Request body: {RequestBody}", requestBodyText);

newRequestBody.Seek(0, SeekOrigin.Begin);
context.Request.Body = newRequestBody;
await _next(context);
}
}

private string FilterSensitiveData(string body)
{
// Logic to filter sensitive data
return body.Replace("password", "[FILTERED]");
}
}

This middleware captures the request body and filters sensitive data before logging it.

Line-by-line breakdown:

  • public async Task Invoke(HttpContext context): The middleware’s main method that processes incoming requests.
  • await context.Request.Body.CopyToAsync(newRequestBody): Copies the original request body for logging.
  • FilterSensitiveData(requestBodyText): Invokes the filtering logic to remove sensitive information.

This approach ensures sensitive data is not logged, mitigating the risk of exposing confidential information.

Edge Cases & Gotchas

While implementing security audit logging, be aware of several potential pitfalls:

1. Failing to Log Critical Events

It's crucial to log all security-relevant events, including access to sensitive endpoints and configuration changes. Failing to do so could leave security gaps.

2. Over-Logging

Excessive logging can lead to performance degradation and increased storage costs. Implementing log levels helps manage the verbosity of logs effectively.

3. Logging Sensitive Information

As mentioned, avoid logging sensitive data. Ensure appropriate data masking is applied before logging.

// Wrong approach
_logger.LogInformation("User password: {Password}", model.Password);

// Correct approach
_logger.LogInformation("User logged in", model.Username);

Performance & Best Practices

To enhance performance and maintain best practices in security audit logging, consider the following:

1. Use Asynchronous Logging

Utilize asynchronous logging to avoid blocking application threads, which can lead to performance issues. Serilog supports asynchronous sinks, ensuring that log writes do not interfere with application responsiveness.

2. Configure Log Retention Policies

Implement log retention policies in Seq to automatically archive or delete old logs. This approach helps manage storage and keeps the log data relevant.

3. Monitor Log Volume

Regularly monitor the volume of logs generated. This helps in identifying unusual spikes that may indicate security incidents.

Real-World Scenario: User Management System

Let’s tie these concepts together in a mini-project: a simple user management system that logs security events.

public class UserManagementController : Controller
{
private readonly ILogger<UserManagementController> _logger;

public UserManagementController(ILogger<UserManagementController> logger)
{
_logger = logger;
}

[HttpPost]
public IActionResult CreateUser(CreateUserViewModel model)
{
if (ModelState.IsValid)
{
// Simulate user creation
_logger.LogInformation("User {Username} created", model.Username);
return RedirectToAction("Index");
}
return View();
}

[HttpPost]
public IActionResult DeleteUser(string username)
{
// Simulate user deletion
_logger.LogInformation("User {Username} deleted", username);
return RedirectToAction("Index");
}
}

This controller demonstrates how to log user creation and deletion events, providing audit trails that can be crucial for compliance and security monitoring.

Key features implemented:

  • Logging of user creation and deletion actions.
  • Structured logs for easy querying in Seq.
  • Basic input validation to ensure proper logging contexts.

Conclusion

  • CWE-778 highlights the importance of robust logging practices to prevent security vulnerabilities.
  • Utilizing Serilog and Seq allows for effective management of security audit logs.
  • Implementing logging best practices is essential to ensure performance and security.
  • Regular monitoring of logs plays a critical role in identifying and responding to security incidents.

S
Shubham Saini
Programming author at Code2Night — sharing tutorials on ASP.NET, C#, and more.
View all posts →

Related Articles

How to Encrypt and Decrypt Password in Asp.Net
May 15, 2022
Exception Handling Asp.Net Core
Aug 05, 2020
HTTP Error 500.31 Failed to load ASP NET Core runtime
Aug 23, 2022
How to implement Paypal in Asp.Net Core
Oct 30, 2022
Previous in ASP.NET Core
CWE-532: Secure Logging in ASP.NET Core - Avoiding Sensitive Data…
Next in ASP.NET Core
CWE-400: Implementing Rate Limiting in ASP.NET Core to Prevent De…
Buy me a pizza

Comments

🔥 Trending This Month

  • 1
    CWE-269: Improper Privilege Management - Implementing the … 411 views
  • 2
    Send Email With HTML Template And PDF Using ASP.Net C# 17,259 views
  • 3
    Building Custom Bedrock Add-Ons with JavaScript: A Complet… 1,943 views
  • 4
    Fix Gemini API Error 429: Quota Exceeded on Free Tier (Sys… 852 views
  • 5
    Error-An error occurred while processing your request in .… 11,972 views
  • 6
    Elasticsearch Integration in ASP.NET Core - Full-Text Sear… 246 views
  • 7
    Mastering Unconditional Statements in C: A Complete Guide … 22,206 views

On this page

🎯

Interview Prep

Ace your ASP.NET Core interview with curated Q&As for all levels.

View ASP.NET Core Interview Q&As

More in ASP.NET Core

  • Task Scheduler in Asp.Net core 18207 views
  • Implement Stripe Payment Gateway In ASP.NET Core 17483 views
  • Send Email With HTML Template And PDF Using ASP.Net C# 17259 views
  • How to implement Paypal in Asp.Net Core 8.0 13445 views
  • HTTP Error 502.5 - ANCM Out Of Process Startup Failure 13390 views
View all ASP.NET Core posts →

Tags

AspNet C# programming AspNet MVC c programming AspNet Core C software development tutorial MVC memory management Paypal coding coding best practices data structures programming tutorial tutorials object oriented programming Slick Slider StripeNet
Free Download for Youtube Subscribers!

First click on Subscribe Now and then subscribe the channel and come back here.
Then Click on "Verify and Download" button for download link

Subscribe Now | 1780
Download
Support Us....!

Please Subscribe to support us

Thank you for Downloading....!

Please Subscribe to support us

Continue with Downloading
Be a Member
Join Us On Whatsapp
Code2Night

A community platform for sharing programming knowledge, tutorials, and blogs. Learn, write, and grow with developers worldwide.

Panipat, Haryana, India
info@code2night.com
Quick Links
  • Home
  • Blog Archive
  • Products
  • Tutorials
  • About Us
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Guest Posts
  • SEO Analyzer
Dev Tools
  • JSON Beautifier
  • HTML Beautifier
  • CSS Beautifier
  • JS Beautifier
  • SQL Formatter
  • Diff Checker
  • Regex Tester
  • Markdown to HTML
  • Word Counter
More Tools
  • Password Generator
  • QR Code Generator
  • Hash Generator
  • Base64 Encoder
  • JWT Decoder
  • UUID Generator
  • Image Converter
  • PNG to ICO
  • SEO Analyzer
By Language
  • Angular
  • Angular js
  • ASP.NET
  • Asp.net Core
  • ASP.NET Core, C#
  • ASP.NET MVC
  • ASP.NET Web Forms
  • C
  • C#
  • C#, ASP.NET Core, Dapper
  • C#, ASP.NET Core, Dapper, Entity Framework
  • DotNet
  • General Web Development
  • HTML, CSS
  • HTML/CSS
  • Java
  • JavaScript
  • JavaScript, HTML, CSS
  • JavaScript, Node.js
  • Node.js
  • Python
  • Python 3.11, Pandas, SQL
  • Python 3.11, SQL
  • Python 3.11, SQLAlchemy
  • Python 3.11, SQLAlchemy, SQL
  • Python 3.11, SQLite
  • React
  • Security
  • SQL Server
  • TypeScript
© 2026 Code2Night. All Rights Reserved.
Made with for developers  |  Privacy  ·  Terms
Translate Page
We use cookies to improve your experience and analyze site traffic. By clicking Accept, you consent to our use of cookies. Privacy Policy
Accessibility
Text size
High contrast
Grayscale
Dyslexia font
Highlight links
Pause animations
Large cursor