1

我有这个包含 Abp 模块的类库(.net 标准),我想通过 AbpCastleLog4NetModule 记录异常。此类库将用于 .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);
        }
    }

谁能帮我提供一个工作样本?

4

1 回答 1

1

我终于在引导程序中找到了我的答案:

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>
于 2020-01-18T08:02:32.720 回答