MicrosoftDocs / winrt-api

WinRT reference content for developing Microsoft Universal Windows Platform (UWP) apps
Creative Commons Attribution 4.0 International
231 stars 500 forks source link

The behavior of the FileLoggingSession is different than described. #2422

Closed krzysztofFC closed 8 months ago

krzysztofFC commented 1 year ago

Issue Observed:

Questions:

Here is a minimal working demo code to illustrate the problem:

#include "pch.h"
#include <iostream>
#include <winrt/Windows.Foundation.h>
#include <winrt/Windows.Foundation.Diagnostics.h>
#include <chrono>
#include <thread>
#include <conio.h>

using namespace winrt;
using namespace Windows::Foundation;
using namespace Windows::Foundation::Diagnostics;

/*
This application creates a FileLoggingSession and a LoggingChannel to log messages
to an ETL file. The Windows.Foundation.Diagnostics documentation states that the
FileLoggingSession class automatically saves logged messages to disk files as they
are logged.

The log files are supposed to be stored in the application's local folder, which is
typically located at:
\AppData\Local\Packages\[PackageFamilyName]\LocalState\Logs

What the application does:
1. Initializes a WinRT apartment.
2. Creates a FileLoggingSession named "MySession".
3. Creates a LoggingChannel named "MyChannel" and adds it to the session.
4. Enters a loop that logs messages every second, appending a counter to indicate the message number.
5. If a key is pressed, the loop exits, stops logging, and triggers the saving of the log file.
6. According to the documentation, the log file should be saved immediately as per the FileLoggingSession's behavior.
*/

int main()
{
    init_apartment();

    // Create a new FileLoggingSession instance.
    FileLoggingSession session(L"MySession");

    // Add a logging channel to the session.
    LoggingChannel channel(L"MyChannel");
    session.AddLoggingChannel(channel);

    // Counter for log messages.
    int counter = 0;

    std::cout << "Logging started. Press any key to stop..." << std::endl;

    // Start a loop to generate logs.
    while (true)
    {
        if (_kbhit()) // Check if a key has been pressed.
        {
            _getch(); // Consume the key press.
            break;    // Exit the loop.
        }

        // Log a message with a counter.
        channel.LogMessage(L"Log message " + std::to_wstring(counter++));

        // Wait for 1 second.
        std::this_thread::sleep_for(std::chrono::seconds(1));
    }

    std::cout << "Stopping logging session. Please wait..." << std::endl;

    // Handle the async operation to save the log file.
    auto saveOperation = session.CloseAndSaveToFileAsync();
    saveOperation.get(); // Wait for the async operation to complete.

    std::cout << "Logging session closed and file saved." << std::endl;

    return 0;
}

Kind regards, Krzysztof


Document Details

Do not edit this section. It is required for learn.microsoft.com ➟ GitHub issue linking.

alvinashcraft commented 1 year ago

Hi @krzysztofFC. Thanks for reaching out about this API. I'll try to find someone on the product team who can explain the observed behavior? Have you tried asking about this on the Q&A forums? The folks there can sometimes provide answers more quickly: Microsoft Q&A

If you do create a thread over at Microsoft Q&A, we would appreciate if you could share that link here so we can track the progress and incorporate any technical details into our documentation.

alvinashcraft commented 8 months ago

I got some info from the product team, @krzysztofFC. They said:

Events go to disk when a buffer is filled or when the user calls FileLoggingSession.CloseAndSaveToFileAsync.

I've updated the docs with this clarification. The changes should appear on the live site soon. Thanks for helping to improve the docs!