---
title: "How to log to a file without using third-party logger in .Net Core?"  
description: "How to log to a file without using third-party logger in .Net Core?"  
author: "Utpal Vishwas"  
published: 2023-07-06  
updated: 2023-07-07  
canonical: https://www.mindstick.com/forum/158984/how-to-log-to-a-file-without-using-third-party-logger-in-dot-net-core  
category: ".net core"  
tags: ["asp.net", "asp.net core", ".net core"]  
reading_time: 2 minutes  

---

# How to log to a file without using third-party logger in .Net Core?

How to [log](https://www.mindstick.com/articles/126269/the-main-uses-of-log-cabins) to a [file](https://www.mindstick.com/articles/59/encrypting-and-decrypting-files-using-c-sharp) without using [third](https://yourviews.mindstick.com/story/4957/the-significance-of-the-third-eye-in-hinduism-more-than-just-a-symbol)-[party](https://yourviews.mindstick.com/view/87403/indian-congress-party-secret-relation-with-china) [logger](https://answers.mindstick.com/qa/92714/what-is-key-logger) in .Net Core?

## Replies

### Reply by Aryan Kumar

To log to a file in .NET Core without using a third-party logger, you can leverage the built-in logging framework provided by the Microsoft.Extensions.Logging namespace. Here's an example of how you can achieve this:

1. Start by creating a new .NET Core Console Application or use an existing one.

2. Add a reference to the Microsoft.Extensions.Logging package to your project. You can do this by adding the following line to your project's .csproj file:

```plaintext
<ItemGroup>
 <PackageReference Include="Microsoft.Extensions.Logging" Version="3.1.0" />
</ItemGroup>
```

Make sure to update the version number to match the version of .NET Core you're using.

3. In your code file (e.g., Program.cs), import the required namespaces:

```plaintext
using Microsoft.Extensions.Logging;
using System;
using System.IO;
```

4. Set up the logger in the `Main` method:

```plaintext
static void Main(string[] args)
{
   var loggerFactory = LoggerFactory.Create(builder =>
   {
       builder.AddConsole(); // Output logs to the console
       builder.AddFile("log.txt"); // Output logs to a file named "log.txt"
   });
   var logger = loggerFactory.CreateLogger<Program>();
   // Use the logger to log messages
   logger.LogInformation("This is an information message");
   logger.LogError("This is an error message");
   Console.ReadLine();
}
```

The `AddConsole` method adds a console logger, while the `AddFile` method adds a file logger and specifies the name of the log file. You can customize the log file path as per your requirements.

5. Run the application, and you should see the log messages displayed in the console as well as written to the specified log file.

This approach allows you to log to a file without relying on a third-party logging library. However, keep in mind that using a dedicated logging library like Serilog or NLog can provide more advanced features and flexibility for logging in your .NET Core application.


---

Original Source: https://www.mindstick.com/forum/158984/how-to-log-to-a-file-without-using-third-party-logger-in-dot-net-core

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
