Logging Additional Exception Info to Database Table

Topics: Data Access Application Block, Exception Handling Application Block, Logging Application Block
Jun 30, 2009 at 6:24 AM

WindowsIdentity :  Computer5\ASPNET ---------DataBase Table

Please let me know the reason for this and tell me the solution.I want WindowsIdentity : ABCCORP\12345 to be in database table also as i am currently getting it in Email.

Thanks,

Ravi

Hi,

We got a problem in Logging an exception information to Database Table.

Additional Info:

MachineName :Computer5

TimeStamp : 6/18/2009 4:47:44 AM

FullName : Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 AppDomainName : /LM/w3svc/1/ROOT/DASH2-35-128897739422383646

ThreadIdentity :

WindowsIdentity : ABCCORP\12345

We got a problem in Logging an exception information to Database Table.The above Additional Info details will get when exception information sent via mail but when we logged the same information to Database we are getting the same information inserted into table but the column WindowsIdentity having the data as Computer5\ASPNET.

FYI,

Default Log Table doesn't have WindowsIdentity column but we have added the column and logic(same logic as  Email one is using) to insert WindowsIdentity data into Log table.But why we are getting different information in two cases.

WindowsIdentity : ABCCORP\12345 --------Email

Jun 30, 2009 at 9:16 AM
Edited Jun 30, 2009 at 9:18 AM

Hi,

Does the two cases happened in the context of exception handling logging (not the regular logging)? And any pointer to repro your problem?

Valiant Dudan
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com

 

Jun 30, 2009 at 9:48 AM
Edited Jun 30, 2009 at 9:50 AM

Another thing, do you have a <authentication> section in your config? can you try adding this <identity impersonate="true" /> line after the authentication section. see what happens.

Valiant Dudan
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com

 

Jun 30, 2009 at 10:49 AM

Thanks Dudan,

Yes.. two cases happened in the context of exception handling logging only.I customized EntLib 4.1 so to rproduce u need all the dll's.I just made few changes in

FormattedDatabaseTraceListener class to insert windows identity in Database Table.

We have these settings in web.config

 

<font size="2" color="#0000ff"><font size="2" color="#0000ff">

<

</font></font><font size="2" color="#0000ff">

 

</font>

authentication mode="Windows" /><font size="2" color="#0000ff"><font size="2" color="#0000ff">

<

</font></font><font size="2" color="#0000ff">

 

</font>

identity impersonate="true" />

Thanks,

Ravi

Jun 30, 2009 at 11:15 AM

Can you post the your modified code for the FormattedDatabaseTraceListener class?

Valiant Dudan
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com

Jun 30, 2009 at 11:26 AM
Edited Jul 1, 2009 at 8:48 AM
This is the corresponding coding..
The code in bold letters was added by me in FormattedDatabaseTraceListener class.
private int ExecuteWriteLogStoredProcedure(LogEntry logEntry, Data.Database db, DbTransaction transaction)
		{

                DbCommand cmd = db.GetStoredProcCommand(writeLogStoredProcName);


                db.AddInParameter(cmd, "eventID", DbType.Int32, logEntry.EventId);
                db.AddInParameter(cmd, "priority", DbType.Int32, logEntry.Priority);
                db.AddParameter(cmd, "severity", DbType.String, 32, ParameterDirection.Input, false, 0, 0, null, DataRowVersion.Default, logEntry.Severity.ToString());
                db.AddParameter(cmd, "title", DbType.String, 256, ParameterDirection.Input, false, 0, 0, null, DataRowVersion.Default, logEntry.Title);
                db.AddInParameter(cmd, "timestamp", DbType.DateTime, logEntry.TimeStamp);
                db.AddParameter(cmd, "machineName", DbType.String, 32, ParameterDirection.Input, false, 0, 0, null, DataRowVersion.Default, logEntry.MachineName);
                db.AddParameter(cmd, "AppDomainName", DbType.String, 512, ParameterDirection.Input, false, 0, 0, null, DataRowVersion.Default, logEntry.AppDomainName);
                db.AddParameter(cmd, "ProcessID", DbType.String, 256, ParameterDirection.Input, false, 0, 0, null, DataRowVersion.Default, logEntry.ProcessId);
                db.AddParameter(cmd, "ProcessName", DbType.String, 512, ParameterDirection.Input, false, 0, 0, null, DataRowVersion.Default, logEntry.ProcessName);
                db.AddParameter(cmd, "ThreadName", DbType.String, 512, ParameterDirection.Input, false, 0, 0, null, DataRowVersion.Default, logEntry.ManagedThreadName);
                db.AddParameter(cmd, "Win32ThreadId", DbType.String, 128, ParameterDirection.Input, false, 0, 0, null, DataRowVersion.Default, logEntry.Win32ThreadId);
                db.AddParameter(cmd, "message", DbType.String, 1500, ParameterDirection.Input, false, 0, 0, null, DataRowVersion.Default, logEntry.Message);
                db.AddParameter(cmd,"windowsIdentity", DbType.String,128,ParameterDirection.Input,false,0,0,null,DataRowVersion.Default, GetWindowsIdentity());//Code added by Me
                
                if (Formatter != null)
                    db.AddInParameter(cmd, "formattedmessage", DbType.String, Formatter.Format(logEntry));
                else
                    db.AddInParameter(cmd, "formattedmessage", DbType.String, logEntry.Message);
                              db.AddOutParameter(cmd, "LogId", DbType.Int32, 4);

                db.ExecuteNonQuery(cmd, transaction);
                 int logId = Convert.ToInt32(cmd.Parameters[cmd.Parameters.Count - 1].Value, CultureInfo.InvariantCulture);                          
                return logId;
		}

       private static string GetWindowsIdentity()
        {
            string windowsIdentity = String.Empty;
             try
            {
                windowsIdentity = WindowsIdentity.GetCurrent().Name;              
              
            }
            catch (SecurityException)
            {
                return "Permission Denied";
            }

                      return windowsIdentity;
        }
Thanks,
Ravi
Jul 1, 2009 at 8:50 AM
Edited Jul 1, 2009 at 8:51 AM

Hi Dudan, please look into this.

Jul 1, 2009 at 8:59 AM

So you also use the same code (WindowsIdentity.GetCurrent().Name) to add the Windows Identity to your email tracelistener log message?  I assume you added it to the Extended Properties?  Who is ABCCORP\12345 and who is Computer5\12345?

 

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

Jul 1, 2009 at 9:36 AM

Thanks Sarah,

Yes .I used the same code which was used in email trace listener log message..I have not added it to extended proprties....The above given are the only changes i made regarding logging an exception to database...

ABCCORP\12345 -----ABCCORP(Domain Name) and 12345(windows Logon Name)--------This i am getting in Email.

Computer5\ASPNET-----Computer5(Machine Name) -------This i am getting in Database..I want the windowsIdentity which i am getting in E-Mail also in Database..(I used the same logic in both cases but getting the different data )

FYI,

I took reference from ExceptionFormatter class of Exception Handling block(Which was used to get Additional Info of the Exception)

Please let me know what is the problem here and how can i solve.Iif you need any other details,please let me know.

Thanks,

Ravi

 

 

Jul 1, 2009 at 10:15 AM
Edited Jul 1, 2009 at 10:19 AM

What happens was that there was an impersonation that occured which causes the current identity to be set to the current logged on user.  Impersonation happens when you try to access local and network resources such as a database.  Obviously, you could set impersonation=false but I wouldn't recommend it since it's a decision that should be made by the one who consciously configured it.   So I would just recommend you to use Page.User.Identity.Name. 

 

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

Jul 1, 2009 at 10:54 AM

Thanks Sarah,

But i am unable to acces Page.User.Identity.Name in FormattedDatabaseTraceListener class as it is a class library not web application/web site.

Please give me the code sample for getting the same..

Thanks,

ravi

Jul 1, 2009 at 12:43 PM

Oh I forgot, sorry about that.  Would adding a reference to System.Web to the class library be acceptable to you?  So you could have - <font size="2">

System.Web.

</font>

HttpContext.Current.User.Identity.Name

 

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

Jul 1, 2009 at 1:43 PM

Thanks sarah,

It solved my problem..