Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SmtpAppender missing in log4net 2.0.8 nuget?

I created a console app for .NET Core 2.0 and installed a log4net 2.0.8 nuget

I want to use the SmtpAppender but it does not exist in the library

This is my log4net configuration

<?xml version="1.0" encoding="utf-8" ?>
<log4net>
   <appender name="SmtpAppender" type="log4net.Appender.SmtpAppender">
    <to value="[email protected]" />
    <from value="[email protected]" />
    <subject type="log4net.Util.PatternString" value="Message logged" />
    <smtpHost value="localhost" />
    <authentication value="None" />
    <port value="25" />
    <bufferSize value="1" />
    <lossy value="false" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%utcdate [%level] - %message%newline%exception" />
    </layout>
  </appender>
  <root>
    <level value="All" />
    <appender-ref ref="SmtpAppender" />
  </root>
</log4net>

And this is Program.cs

namespace Log4NetTest
{
    public class Program
    {
        private static readonly log4net.ILog log = log4net.LogManager.GetLogger(typeof(Program));

        static void Main(string[] args)
        {
            XmlDocument log4netConfig = new XmlDocument();
            log4netConfig.Load(File.OpenRead("log4net.config"));

            var repo = log4net.LogManager.CreateRepository(Assembly.GetEntryAssembly(), 
                typeof(log4net.Repository.Hierarchy.Hierarchy));

            log4net.Config.XmlConfigurator.Configure(repo, log4netConfig["log4net"]);
            log.Info("TEST INFO LOG");
        }

    }
}

When I run the application, I expect an email to be sent to localhost with the message "TEST INFO LOG" but instead log4net outputs this to the console:

log4net:ERROR Could not create Appender [SmtpAppender] of type [log4net.Appender.SmtpAppender]. Reported error follows.
System.TypeLoadException: Could not load type 'log4net.Appender.SmtpAppender' from assembly 'log4net, Version=2.0.8.0, Culture=neutral, PublicKeyToken=669e0ddf0bb1aa2a'.
   at System.Reflection.RuntimeAssembly.GetType(RuntimeAssembly assembly, String name, Boolean throwOnError, Boolean ignoreCase, ObjectHandleOnStack type, ObjectHandleOnStack keepAlive)
   at System.Reflection.RuntimeAssembly.GetType(String name, Boolean throwOnError, Boolean ignoreCase)
   at log4net.Repository.Hierarchy.XmlHierarchyConfigurator.ParseAppender(XmlElement appenderElement)
log4net:ERROR Appender named [SmtpAppender] not found.
Press any key to continue . . .

SmtpAppender type does not even appear in the object browser. There's SmtpPickupDirAppender but that's not what I want to use.

Do I miss something? After some search I did not find anyone ever had this problem, log4net 2.0.8 is 7 months old and downloaded milion times.

like image 324
Mirek Avatar asked Oct 18 '22 04:10

Mirek


1 Answers

Since the built in SmtpAppender is not supported for .Net Core, you can quite easily create your own custom SMTP appender for Log4Net. Take a look at

https://github.com/HatfieldConsultants/Log4Net.Core.CustomAppenders

as an example of how a very basic SMTP appender can be built.

like image 200
Hussain Hassam Avatar answered Oct 21 '22 03:10

Hussain Hassam