Log queries in a file in .net core

Posts   
 
    
Posts: 62
Joined: 14-Feb-2017
# Posted on: 27-May-2020 18:36:18   

Hi,

As mentionned in the documentation, I use this kind of code to log the queries generated by LLBLGEN.

    RuntimeConfiguration.ConfigureDQE<OracleDQEConfiguration>(configuration => configuration
                .SetTraceLevel(TraceLevel.Verbose)
                ...

            RuntimeConfiguration.Tracing
                .SetTraceLevel("ORMGeneral", TraceLevel.Verbose)
                .SetTraceLevel("ORMStateManagement", TraceLevel.Verbose)
                .SetTraceLevel("ORMPersistenceExecution", TraceLevel.Verbose)
                .SetTraceLevel("ORMPlainSQLQueryExecution", TraceLevel.Verbose);

=>I can see the queries in the Output pane.

Now, I would like to be able to log it to a file. I already have a Logger configuration in my appsettings.json as this.

"Logging": {
    "PathFormat": "Logs.txt",
    "LogLevel": {
      "Default": "Debug",
      "Microsoft": "Warning"
    }
  }

=> So my question is : Is it possible to redirect the traces generated by LLBLGEN to the Logger configuration of .NET Core?

Thanks in advance

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 28-May-2020 07:34:05   

LLBLGen writes according to its trace switches on the Trace object. The trace listeners that listen to log information automatically redirects that output to the appropriate outcome. So, the fact that you see the traces in the output window, show that it's working.

Did you configure properly the Logging information in the startup of your app? ( https://docs.microsoft.com/en-us/aspnet/core/fundamentals/logging/?view=aspnetcore-2.2 )

What .NET Core version are you using? Does your Logs.txt get populated with log data other than LLBLGen? Which runtime configuration are you running in (debug, release)?

More about LLBLGen Tracing....

David Elizondo | LLBLGen Support Team
Posts: 62
Joined: 14-Feb-2017
# Posted on: 28-May-2020 09:47:04   

Hi,

LLBLGen writes according to its trace switches on the Trace object. The trace listeners that listen to log information automatically redirects that output to the appropriate outcome. So, the fact that you see the traces in the output window, show that it's working.

Yes, for sure, LLBLGEN is working correctly.

Did you configure properly the Logging information in the startup of your app?

Yes, I use Serilog library extension to be able to configure logs from appsettings : https://nblumhardt.com/2016/10/aspnet-core-file-logger/

loggerFactory.AddFile(configuration.GetSection("Logging"));
  "Logging": {
    "PathFormat": "C:/Logs/Logs.txt",
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning" 
    }

What .NET Core version are you using? Does your Logs.txt get populated with log data other than LLBLGen? Which runtime configuration are you running in (debug, release)?

*.net core 3.1 * I can see microsoft in the logs file and logs I write *debug or release is the same

In fact the only matter is to be able to redirect the LLBLGEN traces to my logger. Because I saw this on the LLBLGEN documentation I thought that I would have to do something similar to redirect logs to my logger.

// add at the start of your program
Trace.Listeners.Add(new TextWriterTraceListener(Console.Out));

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39761
Joined: 17-Aug-2003
# Posted on: 28-May-2020 10:26:19   

I think this will help: https://github.com/serilog-trace-listener/SerilogTraceListener

what you need is a tracelistener, which listens to a tracer in .NET. Our runtime writes to the tracer, and any listener subscribed to it gets that output. So to get the output, you need to register a trace listener with the tracer like Trace.Listeners.Add(new TextWriterTraceListener(Console.Out)); does too, this registers a textwriter tracelistener which writes any text sent to the tracer to the stream specified. With the serilogtracelistener you can write any output send to the tracer to a serilog log (I think, never used it)

Frans Bouma | Lead developer LLBLGen Pro
Posts: 62
Joined: 14-Feb-2017
# Posted on: 28-May-2020 15:09:47   

Hi,

thanks for your answer. Indeed, if I use Trace.Listeners.Add(new ConsoleTraceListener()), logs appears but the SerilogTraceListener doesn't seem to work correctly (not a official Serilog package)

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39761
Joined: 17-Aug-2003
# Posted on: 29-May-2020 09:38:20   

Did you try that SerilogTraceListener I linked to?

Frans Bouma | Lead developer LLBLGen Pro
Posts: 62
Joined: 14-Feb-2017
# Posted on: 04-Jun-2020 10:36:05   

I tried but didn't found way to make it works. I create an issue on the github but Nicholas Blumhardt (it redirects me to stackoverflow. I haven't create an issue in stackoverflow at this time.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39761
Joined: 17-Aug-2003
# Posted on: 05-Jun-2020 10:50:56   

Ok. Well I can't help you either as we don't use serilog, so I don't know why it fails. In any case I think this is what we can do at this point as the trace info is produced and added to the tracer so what happens after that is out of our hands. I'll close this thread now., if you want to reopen it later, just post a message in this thread again simple_smile

Frans Bouma | Lead developer LLBLGen Pro