Search code examples
c#asp.net-boilerplateabp-framework

Use AbpCastleLog4NetModule in Abp Module


I have this Class library(.net standard) which contains an Abp module and I want to log exceptions via AbpCastleLog4NetModule. this class library is going to be used in an .net core console project.

[DependsOn(typeof(ANOTHERMODULE),typeof(AbpCastleLog4NetModule))]
public class MyModule : AbpModule
    {
        public override void PostInitialize()
        {
            var waitTime = 30000;
            timer = new Timer((x) =>
            {
                Integration();
            }, null, 0, waitTime);

        }
        private void Integration()
        {
            var waitTime=30000;
            timer.Change(Timeout.Infinite, Timeout.Infinite);
            foreach (var sftpOptions in optionsCollections)
            {
                try
                {
                    //Do Something

                }
                catch (Exception ex)
                {
                    //log errors
                }
            }
            timer.Change(waitTime, waitTime);
        }
    }

can anyone help me with a working sample?


Solution

  • I Finally find my answer.in the bootstrapper:

    using Abp;
    using Abp.Castle.Logging.Log4Net;
    using Abp.Dependency;
    using Castle.Facilities.Logging;
    using System;
    
    namespace MYNAMESPACE
    {
        class Program
        {
            static void Main(string[] args)
            {
                //Bootstrapping ABP system
                using (var bootstrapper = AbpBootstrapper.Create<MYMODULE>())
                {
                    IocManager.Instance.IocContainer.AddFacility<LoggingFacility>(f => f.UseAbpLog4Net().WithConfig("log.config"));
                    bootstrapper.Initialize();
                    Console.WriteLine("Press enter to exit...");
                    Console.ReadLine();
                }
                Console.ReadLine();
            }
        }
    }
    

    And Inside MyModule I have:

        public class MYMODULE: AbpModule
            {
                Timer timer;
                private static readonly ILog log = LogManager.GetLogger(typeof(MYMODULE));
                public override void PreInitialize()
                {
                    log.Error("MyMessage");
                }
            }
    

    and inside my log.config I can config my setting for logging (for instance I can set configuration for log4net,graylog or both):

        <?xml version="1.0" encoding="utf-8" ?>
        <log4net>
    <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
        <lockingModel type="log4net.Appender.FileAppender+MinimalLock"/>
        <file value="C:\Temp\" />
        <datePattern value="yyyy-MM-dd.'txt'"/>
        <staticLogFileName value="false"/>
        <appendToFile value="true"/>
        <rollingStyle value="Date"/>
        <maxSizeRollBackups value="100"/>
        <maximumFileSize value="15MB"/>
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%date [%thread] %-5level App  %newline %message %newline %newline"/>
        </layout>
      </appender>
          <appender name="GelfHttpAppender" type="Gelf4Net.Appender.GelfHttpAppender, Gelf4Net.HttpAppender">
            <url value="http://MYURL/gelf" />
            <layout type="Gelf4Net.Layout.GelfLayout, Gelf4Net.Core">
              <param name="AdditionalFields" value="app:MYAPP,version:1.0,Environment:Dev,Level:%level" />
              <param name="Facility" value="RandomPhrases" />
              <param name="IncludeLocationInformation" value="true" />
            </layout>
          </appender>
    
          <root>
            <level value="All"/>
            <appender-ref ref="GelfHttpAppender"/>
          </root>
        </log4net>