public async Task AddIncommingAsync <TSupplierType>(string incomingPrefix, TSupplierType supplierType, CancellationToken cancellationToken = default)
        {
            try
            {
                incomingPrefix.NotNullOrWhiteSpace(nameof(incomingPrefix));
                supplierType.NotNull(nameof(supplierType));

                var strSupplierType = supplierType.ConvertTo <string>();
                Logger.SetProperty(LogConstants.SupplierType, strSupplierType);

                var cacheSetting = await CacheSettingService.GetFromCacheBySupplierTypeAsync(strSupplierType, cancellationToken).ConfigureAwait(false);

                var key = incomingPrefix + "-" + Guid.NewGuid().ToString("N");
                await CacheManager.SetAsync(key, string.Empty, cacheSetting.RpmDurationMinutes, cancellationToken).ConfigureAwait(false);

                ////3 times to try
                //if (await TrySet().ConfigureAwait(false) == false)
                //    if (await TrySet().ConfigureAwait(false) == false)
                //        if (await TrySet().ConfigureAwait(false) == false)
                //            throw new Exception($"Faild to set cache {nameof(AddSearchHistoryAsync)}");
                //Task<bool> TrySet() => CacheManager.TrySetAsync(key, string.Empty, cacheSetting.RpmDurationMinutes, cancellationToken);
            }
            catch (Exception ex)
                when(Logger.LogErrorIfNotBefore(ex, "Exception ocurred in {MethodName}", nameof(AddIncommingAsync)))
                {
                    throw;
                }
        }
        public LogOperation(ILogger logger, string value)
        {
            Logger = logger;
            PreviousValue = Logger.GetProperty(LogContext.PropertyKey.Operation);

            // set the new key/value
            if (PreviousValue.HasValue())
            {
                Logger.SetProperty(LogContext.PropertyKey.Operation, PreviousValue + "." + value);
            }
            else
            {
                Logger.SetProperty(LogContext.PropertyKey.Operation, value);
            }
        }
        /// <summary>
        /// Log error if was not logged before
        /// </summary>
        /// <param name="logger">logger</param>
        /// <param name="exception">Exception related to the event.</param>
        /// <param name="messageTemplate">Message template describing the event.</param>
        /// <param name="propertyValues">Objects positionally formatted into the messageTemplate template.</param>
        /// <returns>Return false always to not catch exception in when condation</returns>
        public static bool LogErrorIfNotBefore(this ILogger logger, Exception exception, string messageTemplate, params object[] propertyValues)
        {
            logger.NotNull(nameof(logger));
            exception.NotNull(nameof(exception));
            messageTemplate.NotNull(nameof(messageTemplate));

            if (LogConstants.LogErrorEnabled && !exception.IsLoggedBefore())
            {
                logger.SetProperty(LogConstants.Exception, exception);
                logger.SetProperty(LogConstants.Level, LogLevel.Error);
                logger.Log(LogLevel.Error, exception, messageTemplate, propertyValues, new[] { new LogProperty(LogConstants.LogError, null) });
                exception.MarkAsLogged();
            }
            return(false);
        }
Exemple #4
0
        public async Task <TCacheSetting> GetFromCacheBySupplierTypeAsync <TSupplierType>(TSupplierType supplierType, CancellationToken cancellationToken = default)
        {
            try
            {
                supplierType.NotNull(nameof(supplierType));

                var strSupplierType = supplierType.ConvertTo <string>();

                var cacheKey = string.Format(CacheKeySupplierType, strSupplierType);

                var setting = await CacheManager.GetAsync(cacheKey, async() =>
                {
                    using (await AsyncLock.LockAsync())
                    {
                        var cacheSetting = await Store.TableNoTracking.SingleOrDefaultAsync(p => p.SupplierType == strSupplierType, cancellationToken);

                        if (cacheSetting == null)
                        {
                            cacheSetting = (await Store.TableNoTracking.SingleAsync(p => p.SupplierType == null, cancellationToken)).ShallowCopy <TCacheSetting>();
                            cacheSetting.SupplierType = strSupplierType;
                        }

                        return(cacheSetting);
                    }
                }, 1440, cancellationToken).ConfigureAwait(false);

                Logger.SetProperty(LogConstants.CacheSetting, setting, true);
                return(setting);
            }
            catch (Exception ex)
                when(Logger.LogErrorIfNotBefore(ex, "Exception ocurred in {MethodName}", nameof(GetFromCacheBySupplierTypeAsync)))
                {
                    throw;
                }
        }
 /// <summary>
 /// Clear log context.
 /// </summary>
 internal static void Clear(ILogger logger)
 {
     foreach (var field in PropertyKey.Fields)
     {
         var key = field.GetValue(DummyLogContext) as string;
         logger.SetProperty(key, null);
     }
 }
        /// <summary>
        /// Set all log context values from this LogContext object to logger.
        /// </summary>
        internal void Set(ILogger logger)
        {
            if (Properties == null)
            {
                return;
            }

            foreach (var propertyKeyField in PropertyKey.Fields)
            {
                var propertyKey = propertyKeyField.GetValue(DummyLogContext) as string;

                if (propertyKey != null && Properties.ContainsKey(propertyKey))
                {
                    var propertyValue = Properties[propertyKey];
                    logger.SetProperty(propertyKey, propertyValue);
                }
            }
        }
Exemple #7
0
 private LogProps(ILogger logger, string propertyName, object propertyValue)
 {
     this.logger = logger;
     this.propertyName = propertyName;
     logger.SetProperty(propertyName, propertyValue);
 }
        public async Task AddIncommingAsync <TSupplierType>(string incomingPrefix, TSupplierType supplierType, CancellationToken cancellationToken = default)
        {
            try
            {
                incomingPrefix.NotNullOrWhiteSpace(nameof(incomingPrefix));
                supplierType.NotNull(nameof(supplierType));

                var strSupplierType = supplierType.ConvertTo <string>();
                var hashCode        = incomingPrefix.GetInvariantHashCode();

                Logger.SetProperty(LogConstants.IncomingRequestHashCode, hashCode);
                Logger.SetProperty(LogConstants.SupplierType, strSupplierType);

                var incomingRequest = new IncomingRequest
                {
                    CreatedAt = SystemClock.DateTimeNow,
                    HashCode  = hashCode
                };

                using (await AsyncLock.LockAsync())
                    await IncomingRequestStore.AddAsync(incomingRequest, cancellationToken);
            }
            catch (Exception ex)
                when(Logger.LogErrorIfNotBefore(ex, "Exception ocurred in {MethodName}", nameof(AddIncommingAsync)))
                {
                    throw;
                }
        }
 /// <summary>
 /// Add a property to the log scope if not already present, otherwise, update its value.
 /// </summary>
 /// <param name="logger">logger</param>
 /// <param name="propertyName">The name of the property.</param>
 /// <param name="propertyValue">The value of the property.</param>
 /// <param name="destructureObjects">
 /// Determines whether it must be destruct.
 /// If true, and the value is a non-primitive, non-array type, then the value will be converted to a structure; otherwise, unknown types will be converted to scalars, which are generally stored as strings.
 /// </param>
 public static void SetProperty(this ILogger logger, string propertyName, object propertyValue, bool destructureObjects = false)
 {
     logger.NotNull(nameof(logger));
     logger.SetProperty(new LogProperty(propertyName, propertyValue, destructureObjects));
 }