/// <summary>
        /// Handle notification
        /// </summary>
        /// <param name="notification">Notification to process</param>
        /// <param name="cancellationToken"><see cref="CancellationToken"/> instance</param>
        /// <returns>Handling <see cref="Task"/></returns>
        public Task Handle(TNotification notification,
                           CancellationToken cancellationToken)
        {
            return(Task.Run(() =>
            {
                var typeName = notification?.GetType().FullName;

                if (loggingConfiguration.IsTypeElevatedToWarn(typeName))
                {
                    if (logger.IsEnabled(LogLevel.Warning))
                    {
                        logger.LogWarning(
                            $"Logging notification from {typeName}. Notification details {notification}");
                    }
                }
                // if type is elevated to info log as info
                else if (loggingConfiguration.IsTypeElevatedToInfo(typeName))
                {
                    if (logger.IsEnabled(LogLevel.Information))
                    {
                        logger.LogInformation(
                            $"Logging notification from {typeName}. Notification details {notification}");
                    }
                }
                else if (logger.IsEnabled(LogLevel.Debug))
                {
                    logger.LogDebug($"Logging notification from {typeName}. Notification details {notification}");
                }
            }, CancellationToken.None));
        }
Пример #2
0
        /// <summary>
        /// Process request
        /// </summary>
        /// <param name="request">Request Type</param>
        /// <param name="cancellationToken"><see cref="CancellationToken"/> instance</param>
        /// <returns>Processing <see cref="Task"/></returns>
        public Task Process(TRequest request, CancellationToken cancellationToken)
        {
            return(Task.Run(() =>
            {
                var typeName = request?.GetType().FullName;

                if (loggingConfiguration.IsTypeElevatedToWarn(typeName))
                {
                    if (logger.IsEnabled(LogLevel.Warning))
                    {
                        logger.LogWarning(
                            $"Logging request from {typeName}. Request details {request}");
                    }
                }
                // if type is elevated to info log as info
                else if (loggingConfiguration.IsTypeElevatedToInfo(typeName))
                {
                    if (logger.IsEnabled(LogLevel.Information))
                    {
                        logger.LogInformation(
                            $"Logging request from {typeName}. Request details {request}");
                    }
                }
                else if (logger.IsEnabled(LogLevel.Debug))
                {
                    logger.LogDebug($"Logging request from {typeName}. Request details {request}");
                }
            }, CancellationToken.None));
        }
Пример #3
0
        /// <summary>
        /// Call service loggers before service method invocation
        /// </summary>
        /// <param name="invocation"><see cref="IInvocation"/> instance</param>
        public void PreMethodInvoke(IInvocation invocation)
        {
            // search for custom service logger
            if (serviceLoggers.TryGetValue(invocation.TargetType.FullName, out IServiceLogger serviceLogger) &&
                !disabledServiceLoggers.Contains(serviceLogger.GetType().FullName))
            {
                try
                {
                    // Call logger
                    serviceLogger.PreMethodInvocationLog(invocation);
                }
#pragma warning disable CA1031 // Do not catch general exception types
                catch (Exception e)
#pragma warning restore CA1031 // Do not catch general exception types
                {
                    // Log any errors
                    if (logger.IsEnabled(LogLevel.Error))
                    {
                        logger.LogError(e, $"Service logger { serviceLogger.GetType().FullName} threw an exception " + $"during PreMethodInvocationLog method call. Logger will be disabled");
                    }

                    // Disable logger if any exceptions occur.
                    disabledServiceLoggers.Add(serviceLogger.GetType().FullName);
                }
            }
            else
            {
                // Use default logger if no custom logger is setup
                // Do not log if type is excluded from logging
                if (!loggingConfiguration.IsTypeExcluded(invocation.TargetType.FullName))
                {
                    // If type is elevated to warn log as warn
                    if (loggingConfiguration.IsTypeElevatedToWarn(invocation.TargetType.FullName))
                    {
                        if (logger.IsEnabled(LogLevel.Warning))
                        {
                            logger.LogWarning(
                                $"Logging Interceptor: Calling {invocation.TargetType.FullName}.{invocation.Method.Name} " +
                                $"with parameters: {(invocation.Arguments.Length == 0 ? "None" : string.Join(", ", invocation.Arguments))}",
                                invocation.Arguments);
                        }
                    }
                    // if type is elevated to info log as info
                    else if (loggingConfiguration.IsTypeElevatedToInfo(invocation.TargetType.FullName))
                    {
                        if (logger.IsEnabled(LogLevel.Information))
                        {
                            logger.LogInformation(
                                $"Logging Interceptor: Calling {invocation.TargetType.FullName}.{invocation.Method.Name} " +
                                $"with parameters: {(invocation.Arguments.Length == 0 ? "None" : string.Join(", ", invocation.Arguments))}",
                                invocation.Arguments);
                        }
                    }
                    else if (logger.IsEnabled(LogLevel.Debug))
                    {
                        logger.LogDebug(
                            $"Logging Interceptor: Finished {invocation.TargetType.FullName}.{invocation.Method.Name}. " +
                            $"Returned {invocation.ReturnValue}",
                            invocation.ReturnValue);
                    }
                }
            }
        }