Logging in Windows IoT

Effective logging is crucial for monitoring, debugging, and understanding the behavior of your Windows IoT applications. This section covers various aspects of logging on Windows IoT devices, from basic event logging to advanced diagnostic techniques.

Why Logging is Important for IoT

Windows Event Logging

Windows IoT leverages the robust Windows Event Log system. Applications can write events to various logs, such as the Application log, System log, or custom logs.

Using Event Viewer

You can access and view event logs on your IoT device using the Event Viewer application. For remote access, you can use the Event Viewer on a Windows desktop machine and connect to your IoT device.

Programmatic Logging with Event Tracing for Windows (ETW)

Event Tracing for Windows (ETW) provides a high-performance, low-overhead tracing facility. It's the recommended method for robust logging in Windows IoT.

Basic ETW Logging Example (C#)


using System.Diagnostics.Tracing;

[EventSource(Name = "MyCompany-MyIoTApp")]
public sealed class MyIoTAppEventSource : EventSource
{
    public static MyIoTAppEventSource Log = new MyIoTAppEventSource();

    private MyIoTAppEventSource() { }

    [Event(1, Message = "Application started with device ID: {0}", Level = EventLevel.Informational)]
    public void AppStart(string deviceId)
    {
        WriteEvent(1, deviceId);
    }

    [Event(2, Message = "Processing sensor data: {0}", Level = EventLevel.Verbose)]
    public void ProcessingSensorData(string data)
    {
        WriteEvent(2, data);
    }

    [Event(3, Message = "Error occurred: {0}", Level = EventLevel.Error)]
    public void AppError(string errorMessage)
    {
        WriteEvent(3, errorMessage);
    }
}

// In your application:
// MyIoTAppEventSource.Log.AppStart("Device-XYZ-123");
// MyIoTAppEventSource.Log.ProcessingSensorData("Temperature: 25C");
// MyIoTAppEventSource.Log.AppError("Failed to connect to network.");
        

Note on EventSource

The EventSource class in .NET is the modern way to integrate with ETW. It allows for structured, high-performance logging.

Collecting Logs from Devices

For deployed devices, you'll need mechanisms to collect logs. Options include:

Custom Log Files

While Event Tracing is preferred, some applications might still generate custom log files (e.g., text files). Ensure these are managed efficiently on the device to avoid filling up storage.

Best Practices for Custom Log Files:

Tip: Log Levels

Utilize different log levels (e.g., Verbose, Information, Warning, Error, Critical) to control the verbosity of your logs. This helps in managing log data size and focusing on relevant events during debugging.

Troubleshooting Common Logging Issues

Further Reading

For deeper insights into Windows logging mechanisms, refer to the official Microsoft documentation on: