How do I control logging programmatically

8 posts, 0 answers
  1. Peter
    Peter avatar
    23 posts
    Member since:
    Sep 2008

    Posted 25 Oct 2010 Link to this post

    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
  2. Petko_I
    Admin
    Petko_I avatar
    184 posts

    Posted 28 Oct 2010 Link to this post

    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
  3. DevCraft banner
  4. Peter
    Peter avatar
    23 posts
    Member since:
    Sep 2008

    Posted 01 Nov 2010 Link to this post

    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
  5. Peter
    Peter avatar
    23 posts
    Member since:
    Sep 2008

    Posted 01 Nov 2010 Link to this post

    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.
  6. Petko_I
    Admin
    Petko_I avatar
    184 posts

    Posted 05 Nov 2010 Link to this post

    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
  7. Petko_I
    Admin
    Petko_I avatar
    184 posts

    Posted 12 Nov 2010 Link to this post

    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
  8. Peter
    Peter avatar
    23 posts
    Member since:
    Sep 2008

    Posted 14 Jan 2011 Link to this post

    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
  9. Petko_I
    Admin
    Petko_I avatar
    184 posts

    Posted 20 Jan 2011 Link to this post

    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.
Back to Top
DevCraft banner