---
title: "How do you log all file system operations (create, read, write, delete) in an application?"  
description: "How do you log all file system operations (create, read, write, delete) in an application?"  
author: "ICSM Computer"  
published: 2025-05-15  
updated: 2025-05-15  
canonical: https://www.mindstick.com/interview/34126/how-do-you-log-all-file-system-operations-create-read-write-delete-in-an-application  
category: "c#"  
tags: ["c#", "file handling"]  
reading_time: 5 minutes  

---

# How do you log all file system operations (create, read, write, delete) in an application?

To **log all file system operations (create, read, write, delete)** in a C# application, you need to manually intercept or wrap each file operation since .NET **does not automatically log** them.

Here are common strategies:

### 1. Wrap File Access in a Logging Utility Class

Create a custom static helper class that logs every file operation.

```cs
using System;
using System.IO;

public static class FileLogger
{
    public static string ReadAllText(string path)
    {
        Log("READ", path);
        return File.ReadAllText(path);
    }

    public static void WriteAllText(string path, string content)
    {
        Log("WRITE", path);
        File.WriteAllText(path, content);
    }

    public static void Delete(string path)
    {
        Log("DELETE", path);
        File.Delete(path);
    }

    public static void Create(string path)
    {
        Log("CREATE", path);
        using (File.Create(path)) { }
    }

    private static void Log(string action, string path)
    {
        Console.WriteLine($"{DateTime.Now}: {action} -> {path}");
        // Or write to a log file or database
    }
}
```

### 2. Use `FileSystemWatcher` for External Monitoring

Use `FileSystemWatcher` to detect file changes **in real time** within a directory.

```cs
using System;
using System.IO;

class Watcher
{
    public static void StartWatching(string folderPath)
    {
        var watcher = new FileSystemWatcher(folderPath)
        {
            IncludeSubdirectories = true,
            EnableRaisingEvents = true
        };

        watcher.Created += (s, e) => Console.WriteLine($"Created: {e.FullPath}");
        watcher.Changed += (s, e) => Console.WriteLine($"Changed: {e.FullPath}");
        watcher.Deleted += (s, e) => Console.WriteLine($"Deleted: {e.FullPath}");
        watcher.Renamed += (s, e) => Console.WriteLine($"Renamed: {e.OldFullPath} → {e.FullPath}");
    }
}
```

> Note: `FileSystemWatcher` does not detect **read operations** and can sometimes miss events under high activity.

### 3. Optional: Use Interception or AOP

1. For larger applications:
2. Use **Aspect-Oriented Programming (AOP)** (e.g., PostSharp) to intercept methods.
3. For advanced scenarios, hook into **Windows API** using tools like **ETW (Event Tracing for Windows)** or **Detours** (native-level, not C#).

### Summary

| Method | Captures Read | Write | Delete | Remarks |
| --- | --- | --- | --- | --- |
| Logging Wrapper (C#) | Yes | Yes | Yes | Best control; manual |
| `FileSystemWatcher` | No | Yes | Yes | External, async |
| Windows Audit Policy | Yes | Yes | Yes | System-wide; needs admin |

## Read More

1. [How do you periodically archive old log files based on size or date?](https://www.mindstick.com/forum/161615/how-do-you-periodically-archive-old-log-files-based-on-size-or-date)
2. [What are the best practices to handle large log files in C#?](https://www.mindstick.com/forum/161607/what-are-the-best-practices-to-handle-large-log-files-in-c-sharp)
3. [How do you implement retry logic when reading a file that may be temporarily unavailable?](https://www.mindstick.com/interview/34123/how-do-you-implement-retry-logic-when-reading-a-file-that-may-be-temporarily-unavailable)
4. [How can you restrict file size when writing to a log file?](https://www.mindstick.com/interview/34108/how-can-you-restrict-file-size-when-writing-to-a-log-file)

## Answers

### Answer by ICSM Computer

To **log all file system operations (create, read, write, delete)** in a C# application, you need to manually intercept or wrap each file operation since .NET **does not automatically log** them.

Here are common strategies:

### 1. Wrap File Access in a Logging Utility Class

Create a custom static helper class that logs every file operation.

```cs
using System;
using System.IO;

public static class FileLogger
{
    public static string ReadAllText(string path)
    {
        Log("READ", path);
        return File.ReadAllText(path);
    }

    public static void WriteAllText(string path, string content)
    {
        Log("WRITE", path);
        File.WriteAllText(path, content);
    }

    public static void Delete(string path)
    {
        Log("DELETE", path);
        File.Delete(path);
    }

    public static void Create(string path)
    {
        Log("CREATE", path);
        using (File.Create(path)) { }
    }

    private static void Log(string action, string path)
    {
        Console.WriteLine($"{DateTime.Now}: {action} -> {path}");
        // Or write to a log file or database
    }
}
```

### 2. Use `FileSystemWatcher` for External Monitoring

Use `FileSystemWatcher` to detect file changes **in real time** within a directory.

```cs
using System;
using System.IO;

class Watcher
{
    public static void StartWatching(string folderPath)
    {
        var watcher = new FileSystemWatcher(folderPath)
        {
            IncludeSubdirectories = true,
            EnableRaisingEvents = true
        };

        watcher.Created += (s, e) => Console.WriteLine($"Created: {e.FullPath}");
        watcher.Changed += (s, e) => Console.WriteLine($"Changed: {e.FullPath}");
        watcher.Deleted += (s, e) => Console.WriteLine($"Deleted: {e.FullPath}");
        watcher.Renamed += (s, e) => Console.WriteLine($"Renamed: {e.OldFullPath} → {e.FullPath}");
    }
}
```

> Note: `FileSystemWatcher` does not detect **read operations** and can sometimes miss events under high activity.

### 3. Optional: Use Interception or AOP

1. For larger applications:
2. Use **Aspect-Oriented Programming (AOP)** (e.g., PostSharp) to intercept methods.
3. For advanced scenarios, hook into **Windows API** using tools like **ETW (Event Tracing for Windows)** or **Detours** (native-level, not C#).

### Summary

| Method | Captures Read | Write | Delete | Remarks |
| --- | --- | --- | --- | --- |
| Logging Wrapper (C#) | Yes | Yes | Yes | Best control; manual |
| `FileSystemWatcher` | No | Yes | Yes | External, async |
| Windows Audit Policy | Yes | Yes | Yes | System-wide; needs admin |

## Read More

1. [How do you periodically archive old log files based on size or date?](https://www.mindstick.com/forum/161615/how-do-you-periodically-archive-old-log-files-based-on-size-or-date)
2. [What are the best practices to handle large log files in C#?](https://www.mindstick.com/forum/161607/what-are-the-best-practices-to-handle-large-log-files-in-c-sharp)
3. [How do you implement retry logic when reading a file that may be temporarily unavailable?](https://www.mindstick.com/interview/34123/how-do-you-implement-retry-logic-when-reading-a-file-that-may-be-temporarily-unavailable)
4. [How can you restrict file size when writing to a log file?](https://www.mindstick.com/interview/34108/how-can-you-restrict-file-size-when-writing-to-a-log-file)


---

Original Source: https://www.mindstick.com/interview/34126/how-do-you-log-all-file-system-operations-create-read-write-delete-in-an-application

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
