Skip to content

Incomplete logging to file when using log_query_path option #288

@joostas

Description

@joostas

DuckDB provides local configuration option that lets log all executed queries to file:

Name Description
log_query_path Specifies the path to which queries should be logged (default: NULL, queries are not logged)

When this setting is enabled and queries are executed through DuckDB.NET, the log file records only successful queries - failed queries are not logged. In contrast, when using the DuckDB CLI, both successful and failed queries are logged.

CLI behavior:

DuckDB v1.3.2 (Ossivalis) 0b83e5d2f6
Enter ".help" for usage hints.
Connected to a transient in-memory database.
Use ".open FILENAME" to reopen on a persistent database.
D SET log_query_path = log_cli.txt;
D SELECT 1;
┌───────┐
│   1   │
│ int32 │
├───────┤
│   1   │
└───────┘
D SELECT 1 FROM;
Parser Error:
syntax error at or near ";"

LINE 1: SELECT 1 FROM;
                     ^
D SELECT 1 FROM B;
Catalog Error:
Table with name B does not exist!

LINE 1: SELECT 1 FROM B;
                      ^
D RESET log_query_path;
D .exit

cat .\log_cli.txt

SELECT 1;
SELECT 1 FROM B;

Successful query and failed to execute query are logged. Didn't log failed to parse query.

DuckDB.NET behavior (DuckDBCommand):

   // in public class DuckDBDataReaderTests(DuckDBDatabaseFixture db) : DuckDBTestBase(db)
    [Fact]
    public void LogSqlStatements()
    {
        var suffix = "origin";
        var fileName = $"log_{suffix}.txt";
        if (File.Exists(fileName))
        {
            File.Delete(fileName);
        }

        Command.CommandText = $"SET log_query_path = {fileName}";
        _ = Command.ExecuteNonQuery();
        Command.CommandText = "SELECT 1";
        Command.ExecuteReader();
        Command.CommandText = "SELECT 1 FROM";
        try
        {
            Command.ExecuteReader();
        }
        catch { }

        Command.CommandText = "SELECT 1 FROM B";
        try
        {
            Command.ExecuteReader();
        }
        catch { }

        File.Exists(fileName);
        Command.CommandText = "RESET log_query_path";
        Command.ExecuteReader();
        var logs = File.ReadAllText(fileName);
        logs.Contains("SELECT 1 FROM").Should().BeTrue();
    }

Logs

cat .\DuckDB.NET.Test\bin\Debug\net8.0\log_origin.txt

SELECT 1
RESET log_query_path

Only successful queries were logged.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions