在 Abp 模块中使用 AbpCastleLog4NetModule

Use AbpCastleLog4NetModule in Abp Module

我有这个 Class 库(.net 标准),它包含一个 Abp 模块,我想通过 AbpCastleLog4NetModule 记录异常。 此 class 库将用于 .net 核心控制台项目。

[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);
        }
    }

谁能帮我提供一个工作示例?

我终于找到了我的 answer.in 引导程序:

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();
        }
    }
}

在 MyModule 中我有:

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

在我的 log.config 中,我可以配置我的日志记录设置(例如,我可以为 log4net、graylog 或两者设置配置):

    <?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>