?
Solved

Logic in Enterprise Library .NET 2 Logging Application Block

Posted on 2006-05-01
7
Medium Priority
?
796 Views
Last Modified: 2008-01-09
I am looking for some guidance on how to add logic to the Enterprise Library .NET 2 Logging Application Block.

Basically we are logging exceptions.  What I want to do is if the same exception occurs multiple times within a certain timespan (say 30 seconds) then only log the first instance, not every instance.

Scenario:
Say a database server goes down.  We get notified when a user tries to hit the web application that accesses the database.  However, with a server down - even say for only 5 minutes for a re-boot, we get hundereds of identical messages all within miliseconds of each other.

Now if it is planned we can put a down message up and prevent this from happening, but if something unexpected happens we would prefer to only log the first instance, rather than have our logging flooded.

However, I cannot figure out how to put inlogic that says something like "If the last log entry of this type happend over 30 seconds ago (or if this type of log entry has never happened then log this message and keep track of it.  Else do nothing - it has already been logged"

Even if I couldn't examine type, but had to say the last log entry of any type that would be oaky too.

Please be sure that any answers are for Enterprise Library for .NET 2 Logging Application Block (i.e. TraceListeners, Formatters, etc) and not using the older version (i.e. Sinks)
0
Comment
Question by:mrichmon
  • 5
  • 2
7 Comments
 
LVL 19

Accepted Solution

by:
Jim Cakalic earned 1000 total points
ID: 16580814
You have to create your own LogFilter that can veto logging when your criteria are satisfied. Here's the code included in the Enterprise Library for a MockCustomLogFilter:

//===============================================================================
// Microsoft patterns & practices Enterprise Library
// Logging Application Block
//===============================================================================
// Copyright © Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================

using System;
using System.Collections.Specialized;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Common.Tests.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Logging.Configuration;

namespace Microsoft.Practices.EnterpriseLibrary.Logging.Filters.Tests
{
    [ConfigurationElementType(typeof(CustomLogFilterData))]
    public class MockCustomLogFilter : MockCustomProviderBase, ILogFilter
    {
        public MockCustomLogFilter(NameValueCollection attributes)
            : base(attributes)
        {
        }

        public bool Filter(LogEntry log)
        {
            return true;
        }

        public string Name
        {
            get { return string.Empty; }
        }
    }
}

Your job would be to implement the Filter method so that it returns false when you want logging temporarily disabled. LogEntry has the details on which you can base this decision. Add the custom filter to your app.config.

Regards,
Jim
0
 
LVL 35

Author Comment

by:mrichmon
ID: 16582023
I have tried that.

I tried creating a class like so:

using System;
using System.Configuration;
using System.Collections.Generic;
using System.Text;

using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Logging;
using Microsoft.Practices.EnterpriseLibrary.Logging.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Logging.Filters;

namespace mySpace
{
[ConfigurationElementType(typeof(CustomLogFilterData))]
public class RepetetiveLogFilter : LogFilter
{
      public RepetetiveLogFilter(string name) : base(name) {}
      public override bool Filter(LogEntry entry)
      {
            return true;
      }
}
}

Which compiles perfectly.  However, in Enterprise Library I say new filter of type custom filter.  Then it prompts me to load the dll.  I point to the dll and I get an error saying  "There were no types found in the assembly 'mySpace.dll' that implement or inherit from the base type Microsoft.Practices.EnterpriseLibrary.Logging.Filters.LogFilter"

If I remove something then it is clear that it is inheriting from the correct class as I get an error saying I must implement or override abstract members.  However, it does not seem to be recognized.  So I cannot figure out how to do this.

Also I was thinking that if a filter did work, what I would need to do is store in the filter somehow the previous entries of each type that were logged.  Like maybe in a dictionary that was a static member of the class.  But this is a web application.  Therefore I am not sure if there would be a threading issue to doing it this way.
0
 
LVL 35

Author Comment

by:mrichmon
ID: 16597351
Okay I found it is a known bug in the enterprise library configuation program.  It does not recognize custom built ones unless you move your dll which contains them into the bin directory where the enterprise library configuration program is deployed/installed.  There are some add-ons/patches, but I don't know if they work with version 2, as they say they are for .NET 1.1

I am guessing, but have not confirmed yet, that you just have to make sure the dll is in the bin of the project in its deployment location as well.  I will post back when I discover more.

Also I decided to go with storing a dictionary of the last message in a given category list in the application scope, which can be locked/unlocked to prevent threading / synchronization issues, and also is in a global location.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 35

Author Comment

by:mrichmon
ID: 16597753
A little further info.  After getting the enterprise library configuration to recognize my class by placing in the deployment directory, whenever I run it I get an exception as follows:

****************
Constructor on type 'mySpace.RepetetiveLogFilter' not found.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.MissingMethodException: Constructor on type 'mySpace.RepetetiveLogFilter' not found.
****************

I tried adding a base constructor with no parameters, but that doesn't work either.  So  I don't know what it wants.  I'll post more as I figure out.
0
 
LVL 35

Author Comment

by:mrichmon
ID: 16598617
Okay apparently the mock code is wrong and you can't go based off of using an existing filter as an example either.  One shows a string name constructor only (existing filters) and the other shows a NameValueCOllection attributes:

public MockCustomLogFilter(NameValueCollection attributes)
            : base(attributes)
        {
        }

This shows that it is expecting you accept in a NameValueCollection, however you must send a string into the base, not the attributes collection itself - it doesn't support it.
0
 
LVL 35

Author Comment

by:mrichmon
ID: 16598626
Also the mock code is wrong in that you must have an override in the filter function and shoud not define the Name getter.

Basically using the sample which is posted above DOES NOT WORK.
0
 
LVL 19

Expert Comment

by:Jim Cakalic
ID: 16599712
Maybe because the mock class extends MockCustomProviderBase, not LogFilter? Sorry I wasn't able to get back to you. Glad that you were able to get something working.
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
The Relationships Diagram is a good way to get an overall view of what a database is keeping track of. It is also where relationships are defined. A relationship specifies how two tables connect to each other. As you build tables in Microsoft Ac…
Suggested Courses
Course of the Month12 days, 16 hours left to enroll

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question