question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Inject ILogger to custom class implement IAuditingStore

See original GitHub issue

I create a custom class AuditGrayLogStore to implement IAuditingStore by replace in PreInitialize module:

            Configuration.ReplaceService(typeof(IAuditingStore), () =>
            {
                IocManager.Register<IAuditingStore, AuditGrayLogStore>(DependencyLifeStyle.Transient);
            });

I success log to DB when use this code for AuditGrayLogStore:

using System.Threading.Tasks;
using Abp.Auditing;
using Abp.Dependency;
using Abp.Domain.Repositories;
using Microsoft.Extensions.Logging;

namespace ICO.AuditGrayLog
{
    public class AuditGrayLogStore : IAuditingStore, ITransientDependency
    {
        private readonly IIocResolver _iocResolver;
        private readonly IRepository<AuditLog, long> _auditLogRepository;

        /// <summary>
        /// Creates  a new <see cref="AuditingStore"/>.
        /// </summary>
        public AuditGrayLogStore(
            IRepository<AuditLog, long> auditLogRepository
            )
        {
            _auditLogRepository = auditLogRepository;
        }

        public virtual Task SaveAsync(AuditInfo auditInfo)
        {
            return _auditLogRepository.InsertAsync(AuditLog.CreateFromAuditInfo(auditInfo));
        }
    }
}

But when I inject ILogger like this code:

using System.Threading.Tasks;
using Abp.Auditing;
using Abp.Dependency;
using Abp.Domain.Repositories;
using Microsoft.Extensions.Logging;

namespace ICO.AuditGrayLog
{
    public class AuditGrayLogStore : IAuditingStore, ITransientDependency
    {
        private readonly IIocResolver _iocResolver;
        private readonly IRepository<AuditLog, long> _auditLogRepository;

        /// <summary>
        /// Creates  a new <see cref="AuditingStore"/>.
        /// </summary>
        public AuditGrayLogStore(
            IIocResolver iocResolver,
            IRepository<AuditLog, long> auditLogRepository
            )
        {
            _iocResolver = iocResolver;
            _auditLogRepository = auditLogRepository;
        }

        public virtual Task SaveAsync(AuditInfo auditInfo)
        {
            using (var scope = _iocResolver.CreateScope())
            {
                var GrayLogger = scope.Resolve<ILogger>();
                GrayLogger.LogError("GrayLogger");
            }
            return _auditLogRepository.InsertAsync(AuditLog.CreateFromAuditInfo(auditInfo));
        }
    }
}

Error is : Mvc.ExceptionHandling.AbpExceptionFilter - No component for supporting the service Microsoft.Extensions.Logging.ILogger was found

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
PhanAnh1001commented, Apr 22, 2019

@maliming Thank you , i fixed by follow https://docs.microsoft.com/en-us/aspnet/core/fundamentals/logging/?view=aspnetcore-2.2 when use: public AuditGrayLogStore(ILogger<AuditGrayLogStore> logger)

1reaction
malimingcommented, Apr 22, 2019
  1. Don’t forget to add the “using Abp.Configuration.Startup;” statement since the Configuration.ReplaceService extension method is defined in that namespace.

  2. “i want use this class: Microsoft.Extensions.Logging;” please see: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/logging/?view=aspnetcore-2.2

Read more comments on GitHub >

github_iconTop Results From Across the Web

ILogger Injection into Class
ILogger is a logger that your code can use to write log messages. There are three core methods: IsEnabled tests whether a log...
Read more >
Implement a custom logging provider - .NET
Discover how to implement a custom logging provider with colorized logs, writing custom C# ILogger and ILoggerProvider implementations.
Read more >
Abp Auditing log on DB #242
If I create a custom class like on Abp.Zero "public class AuditingStore : IAuditingStore, ITransientDependency" how will I Fill my AuditInfo ...
Read more >
Audit Logging | Documentation Center | ABP.IO
If you need to save the audit log objects to a custom data store, you can implement the IAuditingStore in your own application...
Read more >
.NET Logging Guide: Part 4 - Custom Logging Providers - ...
In part 4 of the .NET Logging Guide series we break down custom loggers and how to build a logger that logs to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found