How to use a GUID property provider to log to a nullable GUID table column using log4net

StackOverflow https://stackoverflow.com/questions/15979358

  •  03-04-2022
  •  | 
  •  

Question

I have a nullable GUID column in a SQL Server table that I am trying to update using a stored procedure from log4net in my web application.
I am using the property provider approach described in this blog article as a best practice for setting the property in the log4net LogicalThreadContext during Application_BeginRequest to take advantage of the fact that at runtime log4net will call an object's ToString method to get the value without it being clobbered by ASP.NET thread-switching:

protected void Application_BeginRequest() {
    log4net.LogicalThreadContext.Properties["userid"] = new UserIdProvider();

And here is the UserIdProvider:

public class UserIdProvider {
    public Guid IConvertible() { //attempt at resolving error on IConvertible
        if (HttpContext.Current.Request.IsAuthenticated) {
            MembershipUser myObject = Membership.GetUser();
            Guid currentUserID = (Guid)myObject.ProviderUserKey;
            return currentUserID;
        }
        else {
            return Guid.Empty;
        }
    }

    public override string ToString() {
        if (HttpContext.Current.Request.IsAuthenticated) {
            MembershipUser myObject = Membership.GetUser();
            Guid currentUserID = (Guid)myObject.ProviderUserKey;
            return currentUserID.ToString();
        }
        else {
            return null;
        }
    }
}

and the log4net layout property is:

<parameter>
  <parameterName value="@UserID" />
  <dbType value="Guid" />
  <layout type="log4net.Layout.RawPropertyLayout">
    <key value="userid" />
  </layout>
</parameter>

With the userid provider log4net throws an exception:

log4net:ERROR [AdoNetAppender] Exception while writing to database
    System.InvalidCastException: Failed to convert parameter value from a 
    UserIdProvider to a Guid. ---> System.InvalidCastException: Object must 
    implement IConvertible.

After seeing this I added the public Guid IConvertible() bit to the provider, but it was a bit of a stab in the dark.

I also tried seeing if I could try some sleight-of-hand with log4net and tell it the dbType is string to see if it would use the ToString() override and pass the parameter through to the stored procedure, but it resulted in this exception:

log4net:ERROR [AdoNetAppender] Exception while writing to database
    System.InvalidCastException: Failed to convert parameter value from a
    UserIdProvider to a String. ---> System.InvalidCastException: Object 
    must implement IConvertible.

I believe I need to use the RawPropertyLayout to get log4net to pass nulls through. I know this from other properties that are nullable int or nullable datetime.

Is there something wrong with the way I've written the IConvertible method, or is there a different way I need to put the UserIdProvider together to return the GUID?

Était-ce utile?

La solution

Object must implement IConvertible.

You aren't actually implementing IConvertible

Your class needs to be defined like

public class UserIdProvider  : IConvertible
{

...

} 

And when you do that, you will probably find you need to add a few more methods.

See http://msdn.microsoft.com/en-us/library/tyz9cd4z.aspx for more details

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top