This is a migrated thread and some comments may be shown as answers.

How do I control logging programmatically

7 Answers 189 Views
Development (API, general questions)
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Peter
Top achievements
Rank 1
Peter asked on 25 Oct 2010, 02:40 PM
Hi
I want to enable SQL tracing for a _subset_ of our unit tests (and easy to turn on where wanted).  It works fine if I enable it in the config file with
<logging.logEvents>normal</logging.logEvents>
<logging.logEventsToSysOut>true</logging.logEventsToSysOut>
but that's then on all the time which is far too much.
So, I want to enable & disable logging programmatically.  I've tried setting TraceAdapter.Instance.Level (to "normal" / "2" - Reflector reveals that OpenAccess handles either) and adding a listener to TraceAdapter.Listeners, but I can't get it to work (even thourgh TraceAdapter.Tracing reports true).  Does anyone know how to get this to work?

I can't get the <logging.logEventsToTrace> setting to work either (even tried using a TraceListener that just throws exceptions).

Thanks, Peter

7 Answers, 1 is accepted

Sort by
0
Petko_I
Telerik team
answered on 28 Oct 2010, 03:08 PM
Hi Peter,

You can control the logging through the model settings dialog – you can follow this link for reference. When you save your settings in this dialog you can see a generated static method GetBackendConfiguration() for the class extending the OpenAccessContext.

public static BackendConfiguration GetBackendConfiguration()
{
    BackendConfiguration backend = new BackendConfiguration();
    backend.Backend = "mssql";
    backend.Logging.LogEvents = "all";
    backend.Logging.LogEventsToTrace = true;
    backend.Logging.Downloader.Filename = "C:\\Temp\\log";
    backend.Logging.Downloader.EventText = true;
    return backend;
}

It will be used to instantiate a BackendConfiguration. If you insist on supplying different values than the ones specified in the model settings dialog, you can write something like this:

BackendConfiguration configuration = NorthwindOAEntityDiagrams.GetBackendConfiguration();
configuration.Backend = "mssql";
configuration.Logging.LogEvents = "all";
configuration.Logging.LogEventsToTrace = true;
configuration.Logging.Downloader.Filename = "C:\\Temp\\log";
configuration.Logging.Downloader.EventText = true;
             
using (NorthwindOAEntityDiagrams context = new NorthwindOAEntityDiagrams(configuration))
{
    var result = context.Orders.ToList();
}

Where NorthwindOAEntityDiagrams is the name of the context. Here are some links to our online documentation - we suspect you have already looked at the first one.

Using a TraceListener

Model Settings Dialog logging

Do not hesitate to contact us if you have further questions.

Regards,
Petko_I
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Peter
Top achievements
Rank 1
answered on 01 Nov 2010, 02:16 PM
Hi Petko

I think you've misunderstood what I'm after here.  I'll try to be more specific and maybe you can point me in the right direction.

First, I should have said we're using Classic OpenAccess (sorry, thought it was obvious because I included the .config file snippet) and we're setting everything up starting from Database.Get(connectionID).  So, we're not using OpenAccessContext or the model settings dialog and don't have the generated code you refer to.
[Actually, we don't use the OpenAccess GUI at all - we set it all up from our own high level model.]

More importantly, what we want to do is use logging to verify that OpenAccess is behaving as we want it to for specific pieces of code - basically we want to check that our LINQ statements are leading to effective SQL.  We don't want to enable logging globally - that produces far too much data, we'd prefer to turn it on & off at runtime for those specific functions.  Generally we want do this within an NUnit test project, we'll turn logging on in the approriate tests just before running the code we're examining and off afterwards.  From the documentation, it looks like it ought to be possible to do this by setting TraceAdapter.Instance.Level, but I can't get that to work. Doing something along the lines you suggest - changing the backend configuration before connecting to the DB - wouldn't help us because the code for which we want logging enabled doesn't start with that - it wil either start with persistent objects that have been passed to it or get an extent using a scope that's passed to it.  (I'm reluctant to perturb our design just to allow control of logging; even if we did, we'd still end up with logging all the preparatory code, not just the piece we're interested in.)

I've spotted that I can get to the logging configuration from the database instance, so I'll experiment with that (but the documentation for Database.BackendConfiguration says "Gets a copy..." so I suspect that it won't let me change the logging for an active database.

Regards, Peter
0
Peter
Top achievements
Rank 1
answered on 01 Nov 2010, 03:54 PM
As I guessed, I can't control logging using Database.BackendConfiguration.Logging.  If I have
var logging = scope.Database.BackendConfiguration.Logging;
logging.LogEventsToSysOut = true;
then, after the assigment, logging.LogEventsToSysOut is true, but scope.Database.BackendConfiguraiton.Logging.LogEventsToSysOut is false; ie it just changed a copy of the LoggingConfiguration.

More usefully, IObjectScope has a property:
  TextWriter Log {get; set;}

Settng this to a suitable TextWriter (eg Console.Out for a proof of concept), does allow me to get the SQL logged.  That's not quite what I want (the other OpenAccess log data is useful too), but it does give me the most important part.
0
Petko_I
Telerik team
answered on 05 Nov 2010, 11:17 AM
Hi Peter,

Firstly, we would like to apologize for the late response. We are pretty busy with the preparation for the upcoming release.
We would also like to notify you that we are currently investigating possibilities for a workaround so that you do not need to rely on  global settings in the configuration file. Your observations are quite valid in every aspect - you indeed receive a copy of BackendConfigartion and changing the settings after the initialization of the scope does not affect the backend behavior. I admit that I have not paid attention to all the details you provided in your first inquiry and was quick to promote what we offer with the visual designer. I will write again to update you on the status of our research.

Sincerely yours,
Petko_I
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Petko_I
Telerik team
answered on 12 Nov 2010, 07:35 PM
Hello Peter,

We are writing to make a suggestion regarding the logging you would like to use in your unit tests. You can try to dynamically load a database object with a configuration on the fly in a way similar to what the ObjectScopeProvider does in its methods. Here we provide an attached example with a configuration for the ubiquitous Northwind database. We assume that the log level "normal" most closely matches the types of commands you would like to track. There is a simple OpenAccessTracer implemented just for demonstrating purposes.

Let us know if this example covers your needs.

Sincerely yours,
Petko_I
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Peter
Top achievements
Rank 1
answered on 14 Jan 2011, 03:03 PM
Hi

Apologies for not getting back to you sooner - other things took priority and then I was on leave over Christmas / New Year.

Sorry, but your suggestion doesn't help.  Unless I've misunderstood, it's still enabling logging at database connection time whereas I want to toggle it on just before running a particular piece of code (and off again afterwards) so I just get log data for what I'm interested it.

As I said before, setting IObjectScope.Log to (eg) Console.Out is a workaround, but I'd really like the full log (not just the SQL statements) to be controllable dynamically.

Regards, Peter
0
Petko_I
Telerik team
answered on 20 Jan 2011, 09:37 AM
Hello Peter,

You have correctly observed that setting the logging level is done at the database connection time. We have tried to give you an alternative where you can use short-living scopes to trace isolated operations for the initialized scope. Dynamically controlling the logging level just for specific operations is something that cannot be done ate this stage. We will consider your suggestion and investigate further our options for bringing additional control over the logging at runtime.

All the best,
Petko_I
the Telerik team
Accelerate your learning with industry's first Telerik OpenAccess ORM SDK. Download today.
Tags
Development (API, general questions)
Asked by
Peter
Top achievements
Rank 1
Answers by
Petko_I
Telerik team
Peter
Top achievements
Rank 1
Share this question
or