storing log message of different application in a single table

Topics: Exception Handling Application Block
Apr 14, 2010 at 3:07 PM

Hi,

I have multiple application for eg .  App1 , App2 etc. Whenever there is a exception i need to log it to database. Here the problem is i'm not able to differentiate from which application the error has occurred. One solution i see is adding one more column in the logging table as application Id, where i can pass application Id along with exception message and other details. But to do this i need to change stored procedure and even the source code. Please help

 

cheers,

sumeeth

Apr 15, 2010 at 1:13 AM
Edited Apr 15, 2010 at 1:14 AM

Hi,

Will the AppDomainName or Title column does not suit as a solution to your problem?

If you really opt to add new column in your Logging database then to achieve this you will need to extend the Log table to add your new property and also the WriteLog stored proc, but I would think that updating the source code is not necessary instead you can create a custom database trace listener to utilize your new parameter in the stored proc. 

HTH.

Gino Terrado
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com

Apr 15, 2010 at 5:42 AM

Gino,

Thanks for the quick response.

Yes i can utilize AppDomainName. but how Application ID is basically of type GUID. But since  application domain name is read from the system at runtime, how can i set my applicationId to AppDomainName ? Can you please explain me how to create custom database trace listner?

 

regards,

sumeeth

Apr 15, 2010 at 7:00 AM

Is my understanding correct that App1 and App2 is completely different from each other?

What I meant about utilizing the AppDomainName is given that you already have 2 different Application (App1 and App2) then you should also have 2 different values in the AppDomainName column for those Applications. Whereas, this is also applicable if ProcessName will be the basis of the difference between the logs of the 2 App.

But if the case is that what you have is just a multiple instances of your application then utilizing the column I've mentioned may not be applicable to you. 

Here's an FAQ item that discusses how to log to custom database/table which also contain a reference on how to create a Custom Trace Listener.

http://entlib.codeplex.com/wikipage?title=How%20can%20I%20log%20to%20a%20custom%20database%2ftable%3f&referringTitle=EntLib%20FAQ

Let me know if you may have any questions or concerns and if you may need a sample project for the CustomDatabaseTracelistener just send us an email.   

HTH

Gino Terrado
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com

 

Apr 15, 2010 at 7:11 AM

Gino,

Thanks for the quick response.

Here ApplicationID which we generate as GUID. It has nothing to do with the Application domain. You can take it as another property to be added along with the exception message.

I will try with the custom Trace listner. That would be great if you send me sample project of CustomDatabase traceListner. I have been working on Application block from past 2 months. The support i get from you guys is really awesome. Keep up the good work. Thanks a lot.

regards,

sumeeth

Apr 15, 2010 at 7:41 AM

Sample sent :-)

Apr 15, 2010 at 7:53 AM

Gino,

Thank you very much. I was unable to receive the sample to some reason. Please send the sample to emailId which i sent. Thanks once again. :)

regards,

sumeeth

Apr 15, 2010 at 8:35 AM

sent..

Apr 15, 2010 at 9:24 AM

Gino,

Sample looks great. But my question is how can i use this with exception handling component. i use ExceptionPolicy.HandleException(ex,policyName). Here how can i pass my applicationID. How this customdatabaseListner will read the application. 

regards,

sumeeth

Apr 15, 2010 at 9:36 AM

Gino,

ok consider your sample itself. Now i want to log the exception using exception handling component. How can i pass the current user to that?  Please help.  If provide sample project that will be of great.

regards,

sumeeth

Apr 15, 2010 at 12:03 PM

Sorry Sumeeth I initially thought we're just dealing with LAB here I've only just noticed now that it's actually EHAB.

Anyway, I bet the CustomTraceListener sample will still be helpful at some point. 

I think the best approach that you could use in your current situation is to create a CustomExceptionHandler that will directly do the logging in your database. I've also sent a sample project to you on this.

As for the ApplicationId, you can utilize the Exception.Data key/value collection property to add your ApplicationId then use it in the CustomExceptionHandler upon writing it to your table. This is also included in the sample.

Let me know if this is indeed what you need and if you have questions or concerns.

Gino Terrado
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com
  

 

Apr 15, 2010 at 12:34 PM

Gino,

Thanks a lot. 

I will check the sample and let you know if there is any issue. Here i'm actually first Log Exception to MSMQ. Then using Distributor service i log to database. I think i should use ExtendedLogEntry object in the MSMQ distributor service? I think i'm in the right place to get my problem solved. Thank you so much.

regards,

sumeeth

Apr 15, 2010 at 1:23 PM

Gino,

I have another issue. Actually i'm storing configuration in SqlServer database using SQlConfigurationSource. My problem is now if i want to edit the configuration how can i read the configuration from the database and get it in the Entlib utility tool?

regards,

sumeeth

Apr 16, 2010 at 1:09 AM

If you're using the SQLConfigurationSource, the entlib config tool automatically loads your configuration from the database upon opening the config file.  Does this answer your question or I'm not getting your question exactly?

 

Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com
  

Apr 16, 2010 at 5:01 AM

hi sarah,

I'm actually not using entlib config tool to store the configuration to the database.Because i had some issues doing that. So i read the external Xml file and write it to the database. Now i want to read the the Configuration using Entlib. I'm not sure how can i achieve this? please help.

regards,

sumeeth

Apr 16, 2010 at 6:07 AM

But did you use the same schema of the table provided by the sqlconfigurationsource from entlib? 

 

Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com
  

Apr 16, 2010 at 6:40 AM

Yse sarah. 

Apr 16, 2010 at 6:47 AM

"Now i want to read the the Configuration using Entlib" - You can now use then the SqlConfigurationSource.  Open your config using the entlib config tool, Add the Configuration Sources section if you haven't, right click on its node, select New Sql Configuration Source.  Configure its the connection string and the name of stored procedures.  Set it then as the SelectedSource in the Configuration Sources properties window.

Let me know if this is not clear.

Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com
  

Apr 16, 2010 at 7:26 AM

Sarah,

Thanks for the quick response.

I followed the steps what you have mentioned. But configuration is not loaded? do i miss something? 

Apr 16, 2010 at 7:43 AM

Sorry, just found out it doesn't exactly work that way.  The approach overwrite what's in the database.  As a workaround, try creating a new config, and paste this:

<configuration>
<configSections>
    <section name="enterpriseLibrary.ConfigurationSource" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ConfigurationSourceSection, Microsoft.Practices.EnterpriseLibrary.Common, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
  </configSections>
  <enterpriseLibrary.ConfigurationSource selectedSource="Sql Configuration Source">
    <sources>
      <add name="Sql Configuration Source" type="Microsoft.Practices.EnterpriseLibrary.SqlConfigurationSource.SqlConfigurationSource, Microsoft.Practices.EnterpriseLibrary.SqlConfigurationSource, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null"
        connectionString="Integrated Security=true;Initial Catalog=TestDb;Data Source=localhost"
        getStoredProcedure="EntLib_GetConfig" setStoredProcedure="EntLib_SetConfig"
        refreshStoredProcedure="EntLib_GetConfig" removeStoredProcedure="EntLib_RemoveSection" />
      <add name="System Configuration Source" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.SystemConfigurationSource, Microsoft.Practices.EnterpriseLibrary.Common, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
    </sources>
  </enterpriseLibrary.ConfigurationSource>
</configuration>

Make sure to set the connectionString attribute to the correct value.  Save it and re-open using the entlib config tool.

 

Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com
  

Apr 16, 2010 at 7:58 AM

Thanks a lot sarah. This works fine. but i have problem here. I have added one more column. Its ApplicationId. So i have changed the stored procedure as well.

The reason for this was, am storing multiple configuration for different application. Now how can i achieve this to get the configuration based on the appID. I'm trying to sort this problem from past one week.

It ll great if you can help me in this.

regards,

sumeeth 

 

Apr 16, 2010 at 8:01 AM

Oh, so you did not used the same schema.  You would have to modify the stored procedures and the source code of the SqlConfigurationSource project to accomodate your ApplicationId column.

 

Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com
  

Apr 16, 2010 at 8:14 AM

Writing to database with appId is already done. I'm able to write it to the database. only problem i have is reading now.

I dont know where i can pass the parameters to fetch the configuration based on the application Id.

 

regards,

sumeeth

Apr 16, 2010 at 9:15 AM

That is something which you need to add to the existing source code.  You must provide yourself the facility to pass that if you really want to read from the database using entlib config tool.  You can expose the application Id property in the SqlConfigurationSourceElementNode and the SqlConfigurationData and modify the codes that process these objects.

Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com
 

Apr 16, 2010 at 9:32 AM

Thanks a lot sarah. 

Gino had provided me a sample of custom handler for write exception message  to the database. When i used it, i get the following error.

"The requested database Logging is not defined in configuration." I don't know what is missing.

regards,

sumeeth

Apr 16, 2010 at 9:34 AM

You need to have a connection string defined with the name "Logging" in your config.

 

Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com
 

Apr 16, 2010 at 10:03 AM

sarah,

My configuration is stored in database and i have the connection string named logging. i don't know what is wrong ?

This how the connectionstring configuration stored in database

<?xml version="1.0" encoding="utf-16"?><SerializableConfigurationSection>      <clear />      <add name="LocalSqlServer" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient" />      <add name="Connection String" connectionString="Data Source=aaa;Initial Catalog=aaaaa;User ID=sa;Password=aaaa" providerName="System.Data.SqlClient" />      <add name="Logging" connectionString="Data Source=aaaa;Initial Catalog=Logging;User ID=sa;Password=aaaa" providerName="System.Data.SqlClient" />  </SerializableConfigurationSection>

regards,

sumeeth

Apr 16, 2010 at 10:07 AM

Nope, the name of your connection string is "Connection String".  It is the value of name attribute and not the name of the database.

 

Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com
 

Apr 16, 2010 at 10:10 AM

If you see there are 3 connection strings. last one is the Logging

Apr 16, 2010 at 10:14 AM
Edited Apr 16, 2010 at 10:19 AM

I need to know how you read your configuration from the database. 

 

Apr 16, 2010 at 10:19 AM

Yes SqlconfigurationSource stores the Configuration in that way. I used the customtracelistner.

It is working great. It reads the connection string. I dont know why it is not reading the configuration for CustomHandler.

Apr 16, 2010 at 10:22 AM

Could you elaborate how did you read the configuration from the database?  Are you writing and saving it to the configuration file of your host project?  If you did, were you able to succesfully open your config file using the entlib config tool?

 

Apr 16, 2010 at 10:22 AM

 public void GetConfigurationSource(string source, Guid appId, Guid verId)
        {
            switch (source)
            {
                case SourceType.SqlServer:
                    ExceptionConfigurationSource = new SqlConfigurationSource(ConnectionString, GetConfig,
                SetConfig, RefreshSection, RemoveSection, appId, verId);
                    break;
                case SourceType.XmlFile:
                    ExceptionConfigurationSource = new FileConfigurationSource(ConfigPath);
                    break;
            }
        }

Sarah,

i have modified the source code that it reads the confiiguration based on application Id

 

 public void GetConfigurationSource(string source, Guid appId, Guid verId)

        {

            switch (source)

            {

                case SourceType.SqlServer:

                    ExceptionConfigurationSource = new SqlConfigurationSource(ConnectionString, GetConfig,

                SetConfig, RefreshSection, RemoveSection, appId, verId);

                    break;

                case SourceType.XmlFile:

                    ExceptionConfigurationSource = new FileConfigurationSource(ConfigPath);

                    break;

            }

        }

Apr 16, 2010 at 10:25 AM

my exception handling component reads the configuration. there is no problem at all. Only when i added the custom handler there is a problem.

 

Apr 16, 2010 at 10:33 AM
Edited Apr 19, 2010 at 12:40 AM

Ok, but I need to  know what are you doing with the instance of IConfigurationSource you retrieved, how are you supplying it to the custom handler.  Are you using it to create a DatabaseProviderFactory and in turn use that object to create a Database object? Please confirm.

Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com
 

Apr 19, 2010 at 9:14 AM

sarah,

I'm able to log the message to the database using custom handler. I was storing the connection string in the database. I don't somehow it was reading it. so i added connection string in the web config.

it works great now. Thanks for your help guys. I want to know one more thing. When i open the config file having SqlCOnfiguration source using entlib tool, which class is called first in SqlConfigurationSource project?

regards,

sumeeth

 

Apr 19, 2010 at 9:43 AM

Does your question include the ones related to design?  If yes, I'm guessing it's the SqlConfigurationSourceDesignManager as this is where the registering nodes or making nodes out of the settings in the config file starts..  If you're only specific to the SqlConfigurationSource project, I think it's the SqlConfigurationSourceElement.

 

Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com
 

Apr 19, 2010 at 10:08 AM

Sarah,

I have added 2 more columns in the SqlConfiguration source. Now I'm able to see it in the config utility tool.

Now when i try to open the configuration Which is stored in the database, it throws me Argument null exception error.

regards,

sumeeth

Apr 19, 2010 at 11:31 PM

For different topics/questions, please create a new discussion.

As for this, I need more information.  You said you were able to see your configuration using entlib config tool.  I'm not clear on when are you getting the ArgumentNullException error.

 

Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com
 

Apr 20, 2010 at 5:10 AM

hi,

sure i will create new discussion. since you were replying on time, i thought i will continue with this discussion itself. I was able to see the configuration with default Entlib has provided. 

But i have added two more element in the SqlConfigurationSource. My new configuration looks like this.

<configuration>  <configSections>    <section name="enterpriseLibrary.ConfigurationSource" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ConfigurationSourceSection, Microsoft.Practices.EnterpriseLibrary.Common, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" />  </configSections>  <enterpriseLibrary.ConfigurationSource selectedSource="Sql Configuration Source">    <sources>      <add name="Sql Configuration Source" type="Microsoft.Practices.EnterpriseLibrary.SqlConfigurationSource.SqlConfigurationSource, Microsoft.Practices.EnterpriseLibrary.SqlConfigurationSource, Version=2.0.0.0, Culture=neutral, PublicKeyToken=8be4e8f6c7976c96"        connectionString="[Connection string]'"        getStoredProcedure="EntLib_GetConfig" setStoredProcedure="EntLib_SetConfig"        refreshStoredProcedure="UpdateSectionDate" removeStoredProcedure="EntLib_RemoveSection"        applicationId="3A8EAF5E-021E-48E1-AD55-F71EF277B69B" versionId="2547BB0B-F1EA-4208-A282-61FEB4021F85" />    </sources>  </enterpriseLibrary.ConfigurationSource></configuration>

I have modified the stored procedure, such a way that, i can get configuration based on ApplicationId. I'm saving multiple configuration for multiple application. 

Now if i want to open the configuration of particular Application Id using Entlib tool, i get the ArgumentNull exception. My new Stored Proc, needs ApplicationId as the parameter to get the configuration. 

Please let me know if you are not clear with the question.

regards,

sumeeth

 

Apr 20, 2010 at 5:28 AM

You'll  need to include those 2 extra elements you added in the source code, not only in the stored procedure.  At the minimum they should be added to the following classes:

 SqlConfigurationSourceElementNode

SqlConfigurationData

SqlConfigurationParameter

SqlConfigurationSourceElement

All the other code changes you'll need to make will be those codes which creates these objects.  You'll modify them in a way that will include the applicationId and the versionId in their instantiation.

 

Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com