getsentry / sentry-dotnet

Sentry SDK for .NET
https://docs.sentry.io/platforms/dotnet
MIT License
595 stars 207 forks source link

Unable to cast object of type 'Sentry.Log4Net.SentryAppender' to type 'log4net.Appender.IAppender' #3365

Open khsd opened 6 months ago

khsd commented 6 months ago

Package

Sentry.Log4net

.NET Flavor

.NET

.NET Version

4.8.0

OS

Windows

SDK Version

4.6.0

Self-Hosted Sentry Version

No response

Steps to Reproduce

  1. Added the following config to an existing log4net configuration running under Sitecore on .net 4.8
    <log4net>
      <appender name="SentryAppender" type="Sentry.Log4Net.SentryAppender, Sentry.Log4Net">
        <Dsn value="https://**************************************************************" />
        <!--Sends the log event Identity value as the user-->
        <SendIdentity value="true" />
        <Environment value="Test" />
        <bufferSize value="1"/>
        <threshold value="ERROR" />
      </appender>
      <root>
        <appender-ref ref="SentryAppender"/>
      </root>
    </log4net>

Expected Result

No error during initialization.

Actual Result

log4net: DOMConfigurator: Loading Appender [SentryAppender] type: [Sentry.Log4Net.SentryAppender, Sentry.Log4Net]
log4net:ERROR DOMConfigurator: Could not create Appender [SentryAppender] of type [Sentry.Log4Net.SentryAppender, Sentry.Log4Net]. Reported error follows.
System.InvalidCastException: Unable to cast object of type 'Sentry.Log4Net.SentryAppender' to type 'log4net.Appender.IAppender'.
   at log4net.Repository.Hierarchy.DOMHierarchyConfigurator.ParseAppender(XmlElement appenderElement)
log4net:ERROR DOMConfigurator: Appender named [SentryAppender] not found.
bitsandfoxes commented 6 months ago

Hey @khsd, sorry to see you run into issues. I'm not quite sure how to go about this. The SentryAppender inherits from AppenderSkeleton and I don't see where the requirement for log4net.Appender.IAppender comes from. Would you be able to share a minimal repro with us?

jamescrosswell commented 6 months ago

I'm not quite sure how to go about this. The SentryAppender inherits from AppenderSkeleton and I don't see where the requirement for log4net.Appender.IAppender comes from.

AppenderSkeleton implements IAppender. I'm guessing log4net just needs any old IAppender and AppenderSkeleton is a quick way to wire one of these up without a bunch of boilerplate.

khsd commented 6 months ago

Log4net for my project is provided by Sitecore so it maybe this implementation that makes the error. I don't think i can share a project since it would require a license for Sitecore. I will try to debug this some more at this end and report my findings.

jamescrosswell commented 6 months ago

I will try to debug this some more at this end and report my findings.

Something that you might be able to test is whether you have the same issue if specifying the configuration in C# (rather than XML). If you don't, that would indicate a problem with the XML configuration.