---
title: "How to create a log file in C#?"  
description: "How to create a log file in C#?"  
author: "Steilla Mitchel"  
published: 2023-09-19  
updated: 2023-09-25  
canonical: https://www.mindstick.com/forum/159903/how-to-create-a-log-file-in-c-sharp  
category: "c#"  
tags: ["c#", "file", "log file"]  
reading_time: 3 minutes  

---

# How to create a log file in C#?

How to create a [log](https://www.mindstick.com/articles/126269/the-main-uses-of-log-cabins) [file in C#](https://www.mindstick.com/interview/34137/how-do-you-extract-text-content-from-a-pdf-or-docx-file-in-c-sharp)?

## Replies

### Reply by Aryan Kumar

Creating a log [file](https://www.mindstick.com/articles/59/encrypting-and-decrypting-files-using-c-sharp) in C# is a common task for tracking application events, debugging, and monitoring. You can use the `System.IO` namespace to work with files in C#. Here's a basic example of how to create and write to a log file:

```plaintext
using System;
using System.IO;
class Program
{
   static void Main()
   {
       // Specify the path to the log file
       string logFilePath = "log.txt";
       try
       {
           // Create or open the log file for appending
           using (StreamWriter writer = File.AppendText(logFilePath))
           {
               // Log the current date and time along with a message
               writer.WriteLine($"{DateTime.Now} - Application started.");
               // You can log more messages by repeating the WriteLine method
               writer.WriteLine($"{DateTime.Now} - Something happened.");
               // Remember to close the StreamWriter to release resources
               writer.Close();
           }
           Console.WriteLine("Log file created and updated successfully.");
       }
       catch (Exception ex)
       {
           Console.WriteLine($"An error occurred: {ex.Message}");
       }
   }
}
```

In this example:

1. We specify the path to the log file (in this case, "log.txt").

2. Inside a `**try-catch**` block, we create or open the log file using a `**StreamWriter**` in append mode (`File.AppendText`). This ensures that existing log entries are preserved, and new entries are added to the end of the file.

3. We write log messages using the `**WriteLine**` method, including timestamps to indicate when the log entry was created.

4. Finally, we close the `**StreamWriter**` to release resources and handle any exceptions that may occur during file creation or writing.

You can customize this code to suit your logging needs, such as adding more detailed information, configuring log levels, or using logging frameworks like Serilog or log4net for more advanced logging capabilities.

### Reply by Gulshan Negi

Well, in C# here is the simple example on how to create [log file](https://www.mindstick.com/articles/327201/create-log-file-in-c-sharp).

`using System;`\
`using System.IO;`

`class Program`\
`{`\
`static void Main()`\
`{`\
`string logFilePath = "log.txt"; // Specify the path for your log file`

`// Example log messages`\
`Log("This is an informational message.", logFilePath);`\
`Log("An error occurred: 404 Not Found.", logFilePath);`\
`}`

`static void Log(string message, string logFilePath)`\
`{`\
`try`\
`{`\
`// Create or open the log file in append mode`\
`using (StreamWriter sw = File.AppendText(logFilePath))`\
`{`\
`// Create a log entry with a timestamp`\
`string logEntry = $"{DateTime.Now:yyyy-MM-dd HH:mm:ss} - {message}";`\
``\
`// Write the log entry to the file`\
`sw.WriteLine(logEntry);`\
``\
`// Optionally, you can also write to the console for debugging purposes`\
`Console.WriteLine(logEntry);`\
`}`\
`}`\
`catch (Exception ex)`\
`{`\
`// Handle any exceptions that may occur while logging`\
`Console.WriteLine($"Error writing to the log file: {ex.Message}");`\
`}`\
`}`\
`}`\

Thanks


---

Original Source: https://www.mindstick.com/forum/159903/how-to-create-a-log-file-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
