0

由于我对 PostSharp(最新版本)知之甚少,我有 2 个错误。我的项目的三个方面类,但我只得到 2 个错误。当 getall 方法在我的 mvc 项目中运行时,我希望在我的数据库和 C:/Log/Log.txt 中生成日志信息。但是没有创建日志。这是我想从你这里得到的。无论我如何编写代码块,我的问题都解决了吗?我有一些验证和交易流程,但我认为这与我遇到的错误没有任何关系,因此无需详细说明。首先,我收到以下警告。

在此处输入图像描述

为了解决这个问题,我按照以下步骤操作。LogAspect 和 FluentValidationAspect 类一直出错。

[LogAspect(AspectPriority = 1), FluentValidationAspect(AspectPriority = 2), TransactionScopeAspect(AspectPriority = 3)]

再一次,我得到了以下错误。(CS 7036)

在此处输入图像描述

我想在我的 BookManager 类中做一些操作。(日志、验证、交易)。这是代码;

 [LogAspect(AspectPriority = 1), FluentValidationAspect(AspectPriority = 2), TransactionScopeAspect(AspectPriority = 3)]
public class BookManager : IBookService
{

    private  IBookDal _bookDal;
    private  IPersonDal _personDal;
    
    /* private readonly IQueryableRepository<Book> _queryable; */
    public BookManager(IBookDal bookDal, IPersonDal personDal/*IQueryableRepository<Book> queryable */)
    {
        _personDal = personDal;
        /*_queryable = queryable; */
        _bookDal = bookDal;
    }
   [FluentValidationAspect(typeof(BookValidator))]
    
    public Book Add(Book book)
    {
        return _bookDal.Add(book);
    }

    public void Delete(Book book)
    {
        _bookDal.Delete(book);
    }
    [LogAspect(typeof(DatabaseLogger))]
    public List<Book> GetAll()
    {
        return _bookDal.GetList();
    }
    [TransactionScopeAspect]
    public void TransactionalOperation(Person person, Book book)
    {
        _personDal.Delete(person);
        // Business Codes
        _bookDal.Add(book);
    }

    public Book GetById(int bookId)
    {
        return _bookDal.Get(p=>p.BookId==bookId);
    }
   [FluentValidationAspect(typeof(BookValidator))]
    public Book Update(Book book)
    {
        return _bookDal.Update(book);
    }
}
[Serializable]
[MulticastAttributeUsage(MulticastTargets.Method, TargetMemberAttributes = MulticastAttributes.Instance)]

public class LogAspect : OnMethodBoundaryAspect
{
    
    private  Type _loggerType;
    [NonSerialized]
    private LoggerService _loggerService;
    public LogAspect(Type loggerType)
    {
        _loggerType = loggerType;
    }

    public override void RuntimeInitialize(MethodBase method)
    {

        if (_loggerType.BaseType != typeof(LoggerService))
        {
            throw new Exception("Wrong logger type.");
        }
        
        _loggerService = (LoggerService)Activator.CreateInstance(_loggerType);
        base.RuntimeInitialize(method);
    }
    public override void OnEntry(MethodExecutionArgs args)
    {
        if (!_loggerService.IsInfoEnabled)
        {
            return;
        }
        try
        {
            var logParameters = args.Method.GetParameters().Select((t, i) => new LogParameter
            {
                Name = t.Name,
                Type = t.ParameterType.Name,
                Value = args.Arguments.GetArgument(i)
            }).ToList();
            var logDetail = new LogDetail
            {
                FullName = args.Method.DeclaringType == null ? null : args.Method.DeclaringType.Name,
                MethodName = args.Method.Name,
                Parameters = logParameters
            };
            _loggerService.Info(logDetail);
        }
        catch (Exception)
        {

            
        }
        
    }
}
 [Serializable]
public class FluentValidationAspect : OnMethodBoundaryAspect
{
    Type _validatorType;
    public FluentValidationAspect(Type validatorType)
    {
        _validatorType = validatorType;
    }
    public override void OnEntry(MethodExecutionArgs args)
    {
        var validator = (IValidator)Activator.CreateInstance(_validatorType);
        var entityType = _validatorType.BaseType.GetGenericArguments()[0];
        var entities = args.Arguments.Where(t => t.GetType() == entityType);
        foreach (var entity in entities)
        {
            ValidatorTool.FluentValidate(validator, entity);
        }
    }
}

我想告诉你一些你必须考虑的事情。另外,我做了汇编级别的日志记录。这是代码。

[assembly: LogAspect(typeof(JsonFileLogger), AttributeTargetTypes = "ELibrary.Library.Business.Managers.BookManager*")]

最后我想补充一下,能否通过重写错误的块来解释这个错误的解决方案?

4

1 回答 1

0

此处记录了订购方面。您收到警告是因为 PostSharp 没有有关某些转换顺序的信息。在您的情况下,我会在上添加以下属性LogAspect

[AspectTypeDependency(AspectDependencyAction.Order, AspectDependencyPosition.Before, typeof(FluentValidationAspect))]

然后在FluentValidationAspect

[AspectTypeDependency(AspectDependencyAction.Order, AspectDependencyPosition.Before, typeof(TransactionScopeAspect))]

这应该完全排序这三个方面,你应该摆脱警告。

之后,您会遇到 C# 错误,因为您的方面构造函数只是有一个您没有指定的参数。需要在各个属性上指定 AspectPriority。

于 2021-06-21T09:41:17.057 回答