SmtpAppender missing in log4net 2.0.8 nuget?

倖福魔咒の 提交于 2020-01-02 04:53:09

问题


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="test@test.com" />
    <from value="test@test.com" />
    <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.


回答1:


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.



来源:https://stackoverflow.com/questions/46862350/smtpappender-missing-in-log4net-2-0-8-nuget

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!