Exemple #1
0
        public IDisposable ParseBeginScope <T>(T state)
        {
            if (_options.CaptureMessageProperties)
            {
                if (state is IList <KeyValuePair <string, object> > scopePropertyList)
                {
                    return(ScopeProperties.CaptureScopeProperties(scopePropertyList));
                }

                if (!(state is string))
                {
                    if (state is System.Collections.IEnumerable scopePropertyCollection)
                    {
                        return(ScopeProperties.CaptureScopeProperties(scopePropertyCollection, _scopeStateExtractors));
                    }
                    else
                    {
                        return(ScopeProperties.CaptureScopeProperty(state, _scopeStateExtractors));
                    }
                }
                else
                {
                    return(NestedDiagnosticsLogicalContext.Push(state));
                }
            }

            return(CreateDiagnosticLogicalContext(state));
        }
        public static TRepo CreateWriteableRepo <

            TRepo, TEntity, TContext, T>(ConfigurationFactory <T> factory,
                                         string testUser = DEFAULT_USER
                                         )
            where TEntity : class, IHasSysUser, new()
            where TContext : DbContext
            where TRepo : WriteableRepo <TEntity, TContext>
            where T : class
        {
            var databaseName = factory.Configuration.GetDatabaseName <TContext>();
            var instanceName = Guid.NewGuid().ToString();

            TestDbContextManager <TContext> .CreateInMemoryDatabase(
                databaseName, instanceName, out DbContextOptions <TContext> options,
                out TContext context);

            var scopeProperties = new ScopeProperties {
                User = testUser
            };


            scopeProperties.OtherProperties.Add("InstanceName", instanceName);
            scopeProperties.OtherProperties.Add("DbContextOptions", options);

            var repo = Activator.CreateInstance(typeof(TRepo),
                                                new object[] { context, scopeProperties }) as TRepo;

            return(repo);
        }
        /// <summary>
        /// Begin a scope. Use in config with ${ndlc}
        /// </summary>
        /// <param name="state">The state (message)</param>
        /// <returns></returns>
        public IDisposable BeginScope <TState>(TState state)
        {
            if (state == null)
            {
                throw new ArgumentNullException(nameof(state));
            }

            if (_options.CaptureMessageProperties)
            {
                if (state is IEnumerable <KeyValuePair <string, object> > messageProperties)
                {
                    ScopeProperties scope = new ScopeProperties();

                    foreach (var property in messageProperties)
                    {
                        if (string.IsNullOrEmpty(property.Key))
                        {
                            continue;
                        }

                        scope.AddProperty(property.Key, property.Value);
                    }

                    scope.AddDispose(NestedDiagnosticsLogicalContext.Push(state));
                    return(scope);
                }
            }

            return(NestedDiagnosticsLogicalContext.Push(state));
        }
Exemple #4
0
            public static ScopeProperties CaptureScopeProperties(System.Collections.IEnumerable scopePropertyCollection,
                                                                 ConcurrentDictionary <Type, KeyValuePair <Func <object, object>, Func <object, object> > > stateExractor)
            {
                var scope = new ScopeProperties();

                var keyValueExtractor = default(KeyValuePair <Func <object, object>, Func <object, object> >);

                foreach (var property in scopePropertyCollection)
                {
                    if (property == null)
                    {
                        break;
                    }

                    if (keyValueExtractor.Key == null)
                    {
                        if (!TryLookupExtractor(stateExractor, property.GetType(), out keyValueExtractor))
                        {
                            break;
                        }
                    }

                    AddKeyValueProperty(scope, keyValueExtractor, property);
                }

                scope.AddDispose(CreateDiagnosticLogicalContext(scopePropertyCollection));
                return(scope);
            }
 public AgencyInvestigatorCheckRepo(
     AgencyInvestigatorCheckContext context,
     AgencyInvestigatorCheckHistoryContext historyContext,
     ScopeProperties scopeProperties)
     : base(context, historyContext, scopeProperties)
 {
 }
 public DefaultPoliciesApiClient(HttpClient client, IConfiguration config,
                                 ScopeProperties scopeProperties, IdentityServerApi identityServerClient,
                                 SecureTokenCache secureTokenCache, IHostingEnvironment hostingEnvironment
                                 ) : base(client, config, scopeProperties,
                                          identityServerClient,
                                          secureTokenCache, hostingEnvironment)
 {
 }
 public InternalApi2(HttpClient client,
                     IConfiguration config,
                     ScopeProperties scopeProperties,
                     IdentityServer identityServer,
                     SecureTokenCache tokenCache,
                     IHostingEnvironment env
                     ) :
     base(client, config, scopeProperties,
          identityServer, tokenCache, env)
 {
 }
            public static IDisposable CaptureScopeProperty <TState>(TState scopeProperty, ConcurrentDictionary <Type, KeyValuePair <Func <object, object>, Func <object, object> > > stateExractor)
            {
                if (!TryLookupExtractor(stateExractor, scopeProperty.GetType(), out var keyValueExtractor))
                {
                    return(CreateDiagnosticLogicalContext(scopeProperty));
                }

                var scope = new ScopeProperties();

                AddKeyValueProperty(scope, keyValueExtractor, scopeProperty);
                scope.AddDispose(CreateDiagnosticLogicalContext(scopeProperty));
                return(scope);
            }
 private static void AddKeyValueProperty(ScopeProperties scope, KeyValuePair <Func <object, object>, Func <object, object> > keyValueExtractor, object property)
 {
     try
     {
         var propertyKey   = keyValueExtractor.Key.Invoke(property);
         var propertyValue = keyValueExtractor.Value.Invoke(property);
         scope.AddProperty(propertyKey?.ToString() ?? string.Empty, propertyValue);
     }
     catch (Exception ex)
     {
         InternalLogger.Debug(ex, "Exception in BeginScope add property");
     }
 }
Exemple #10
0
        public static TClient CreateWriteableClient <TClient, TStartup>(
            ApiLauncherFactory <TStartup> factory, string testUser = TestRepoFactory.DEFAULT_USER)
            where TClient : ApiClient
            where TStartup : class
        {
            var httpClient = HttpClientFactory.Create();

            var port        = factory.Port;
            var baseAddress = $"http://localhost:{port}";

            var baseAddressConfig = new List <KeyValuePair <string, string> >();
            var clientName        = typeof(TClient).Name;

            baseAddressConfig.Add(new KeyValuePair <string, string>($"Apis:{clientName}:BaseAddress", baseAddress));

            var apiConfiguration = new ConfigurationBuilder()
                                   .AddInMemoryCollection(baseAddressConfig)
                                   .Build();

            var instanceName    = Guid.NewGuid().ToString();
            var scopeProperties = new ScopeProperties {
                User = testUser
            };

            var apiClientHeaders = new List <KeyValuePair <string, StringValues> > {
                new KeyValuePair <string, StringValues>(
                    Interceptor.HDR_USE_INMEMORY, instanceName)
            };

            var dict = new Dictionary <string, object> {
                { Web.ApiClient.HEADER_KEY, apiClientHeaders }
            };

            scopeProperties.OtherProperties.Add(
                typeof(TClient).Name, dict);


            var apiClient = Activator.CreateInstance(typeof(TClient),
                                                     httpClient, apiConfiguration, scopeProperties
                                                     ) as TClient;


            bool ping = httpClient.Ping(TIMEOUT_SECONDS);

            if (ping == false)
            {
                ping = httpClient.Ping(TIMEOUT_SECONDS);
            }

            return(apiClient);
        }
Exemple #11
0
        public SecureApiClient(HttpClient httpClient,
                               IConfiguration config,
                               ScopeProperties scopeProperties,
                               ApiClient identityServerApiClient,
                               SecureTokenCache secureTokenCache,
                               IHostingEnvironment hostingEnvironment)
            : base(httpClient, config, scopeProperties)
        {
            _secureTokenCache        = secureTokenCache;
            _identityServerApiClient = identityServerApiClient;
            _hostingEnvironment      = hostingEnvironment;

            SetBearerToken(config).Wait();
        }
        /// <summary>
        /// Begin a scope. Use in config with ${ndlc}
        /// </summary>
        /// <param name="state">The state (message)</param>
        /// <returns></returns>
        public IDisposable BeginScope <TState>(TState state)
        {
            if (state == null)
            {
                throw new ArgumentNullException(nameof(state));
            }

            if (_options.CaptureMessageProperties && state is IEnumerable <KeyValuePair <string, object> > messageProperties)
            {
                return(ScopeProperties.CreateFromState(state, messageProperties));
            }

            return(NestedDiagnosticsLogicalContext.Push(state));
        }
        private void TestGetAvailableScopes(string reservationOrderId, string reservationId)
        {
            HttpMockServer.RecordsDirectory = GetSessionsDirectoryPath();
            using (MockContext context = MockContext.Start(this.GetType()))
            {
                var reservationsClient = ReservationsTestUtilities.GetAzureReservationAPIClient(context, new RecordedDelegatingHandler {
                    StatusCodeToReturn = HttpStatusCode.OK
                });

                var scope = new ScopeProperties($"/subscriptions/{Common.SubscriptionId}");
                var body  = new SubscriptionScopeProperties(scope);
                var res   = reservationsClient.Reservation.AvailableScopes(reservationOrderId, reservationId, body);
            }
        }
Exemple #14
0
        public CreateOrUpdateDHCPv6ScopeRequest GetRequest()
        {
            var request = new CreateOrUpdateDHCPv6ScopeRequest
            {
                Name              = GenerellProperties.Name,
                Description       = GenerellProperties.Description,
                ParentId          = GenerellProperties.HasParent == false ? new Guid?() : GenerellProperties.ParentId,
                AddressProperties = AddressRelatedProperties.GetRequest(),
                Properties        = ScopeProperties.GetRequest(),
                Resolver          = ResolverProperties.GetRequest(),
            };

            return(request);
        }
            public static IDisposable CreateFromState <TState>(TState state, IEnumerable <KeyValuePair <string, object> > messageProperties)
            {
                ScopeProperties scope = new ScopeProperties();

                foreach (var property in messageProperties)
                {
                    if (String.IsNullOrEmpty(property.Key))
                    {
                        continue;
                    }

                    scope.AddProperty(property.Key, property.Value);
                }

                scope.AddDispose(NestedDiagnosticsLogicalContext.Push(state));
                return(scope);
            }
Exemple #16
0
            public static IDisposable CreateFromState(IEnumerable <KeyValuePair <string, object> > messageProperties)
            {
                var scope = new ScopeProperties();

                //var stateString = string.Empty;
                foreach (var property in messageProperties.ToArray())
                {
                    if (string.IsNullOrEmpty(property.Key))
                    {
                        continue;
                    }
                    //stateString += $"{property.Key}:{property.Value} ";
                    scope.AddProperty(property.Key, property.Value);
                }
                scope.AddDispose(NestedDiagnosticsLogicalContext.Push(messageProperties));
                return(scope);
            }
            public static ScopeProperties CaptureScopeProperties(IReadOnlyList <KeyValuePair <string, object> > scopePropertyList)
            {
                ScopeProperties scope = new ScopeProperties(scopePropertyList.Count + 1);

                for (int i = 0; i < scopePropertyList.Count; ++i)
                {
                    var property = scopePropertyList[i];
                    if (i == scopePropertyList.Count - 1 && i > 0 && property.Key == NLogLogger.OriginalFormatPropertyName)
                    {
                        continue;   // Handle BeginScope("Hello {World}", "Earth")
                    }
                    scope.AddProperty(property.Key, property.Value);
                }

                scope.AddDispose(CreateDiagnosticLogicalContext(scopePropertyList));
                return(scope);
            }
Exemple #18
0
        public ApiClient(HttpClient httpClient, IConfiguration config, ScopeProperties scopeProperties)
        {
            HttpClient      = httpClient;
            ScopeProperties = scopeProperties;
            Configuration   = config;

            //build headers from ApiClientHeaders entry in scopeProperties
            foreach (var targetProp in scopeProperties.OtherProperties.Where(x => x.Key == this.GetType().Name))
            {
                var dict = targetProp.Value as Dictionary <string, object>;
                foreach (var prop in dict)
                {
                    var headers = prop.Value as List <KeyValuePair <string, StringValues> >;
                    foreach (var header in headers)
                    {
                        foreach (var value in header.Value)
                        {
                            httpClient.DefaultRequestHeaders.Add(header.Key, value.ToString());
                        }
                    }
                }
            }

            //add X-User header from ScopeProperties, if the header doesn't already exist.
            var hdrs            = httpClient.DefaultRequestHeaders;
            var userHeaderCount = hdrs.Where(x => x.Key == "X-User").Count();

            if (userHeaderCount == 0 && !string.IsNullOrWhiteSpace(ScopeProperties.User))
            {
                hdrs.Add("X-User", ScopeProperties.User);
            }


            var baseAddress = config[$"Apis:{GetType().Name}:BaseAddress"];
            var env         = config["ASPNETCORE_ENVIRONMENT"];

            if (string.IsNullOrEmpty(baseAddress))
            {
                throw new ApplicationException($"Cannot find entry for 'Apis:{GetType().Name}:BaseAddress' in the configuration (e.g., appsettings.{env}.json).  Each ApiClient and SecureApiClient must have an entry in the configuration that corresponds to the class name of the ApiClient or SecureApiClient.");
            }


            HttpClient.BaseAddress = new Uri(baseAddress);
        }
        public static TRepo CreateWriteableTemporalRepo <
            TRepo, TEntity, TContext, THistoryContext, T>(ConfigurationFactory <T> factory,
                                                          string testUser = DEFAULT_USER
                                                          )

            where TEntity : class, IEFCoreTemporalModel, new()
            where TContext : DbContext
            where THistoryContext : DbContext
            where TRepo : WriteableTemporalRepo <TEntity, TContext, THistoryContext>
            where T : class
        {
            var databaseName = factory.Configuration.GetDatabaseName <TContext>();
            var instanceName = Guid.NewGuid().ToString();

            var historyDatabaseName = factory.Configuration.GetDatabaseName <THistoryContext>();
            var historyInstanceName = instanceName + Interceptor.HISTORY_INSTANCE_SUFFIX;


            TestDbContextManager <TContext> .CreateInMemoryDatabase(
                databaseName, instanceName, out DbContextOptions <TContext> options,
                out TContext context);

            TestDbContextManager <THistoryContext> .CreateInMemoryDatabase(
                historyDatabaseName, historyInstanceName,
                out DbContextOptions <THistoryContext> historyOptions,
                out THistoryContext historyContext);

            var scopeProperties = new ScopeProperties {
                User = testUser
            };

            scopeProperties.OtherProperties.Add("InstanceName", instanceName);
            scopeProperties.OtherProperties.Add("HistoryInstanceName", historyInstanceName);
            scopeProperties.OtherProperties.Add("DbContextOptions", options);
            scopeProperties.OtherProperties.Add("HistoryDbContextOptions", historyOptions);


            var repo = Activator.CreateInstance(typeof(TRepo),
                                                new object[] { context, historyContext, scopeProperties }) as TRepo;

            return(repo);
        }
        public IDisposable ParseBeginScope <T>(T state)
        {
            if (_options.CaptureMessageProperties)
            {
                if (state is IReadOnlyList <KeyValuePair <string, object> > scopePropertyList)
                {
                    return(ScopeProperties.CaptureScopeProperties(scopePropertyList, _options.IncludeActivtyIdsWithBeginScope));
                }

                if (!(state is string))
                {
                    if (state is IEnumerable scopePropertyCollection)
                    {
                        return(ScopeProperties.CaptureScopeProperties(scopePropertyCollection, _scopeStateExtractors));
                    }

                    return(ScopeProperties.CaptureScopeProperty(state, _scopeStateExtractors));
                }
            }

            return(NestedDiagnosticsLogicalContext.Push(state));
        }
Exemple #21
0
 public SmtpConfigRepo(EmmaContext context, EmmaHistoryContext historyContext, ScopeProperties scopeProperties)
     : base(context, historyContext, scopeProperties)
 {
 }
Exemple #22
0
 public PositionRepo(HrContext context, HrHistoryContext historyContext,
                     ScopeProperties scopeProperties)
     : base(context, historyContext, scopeProperties)
 {
 }
 public AgencyOnlineCheckRepo(
     AgencyOnlineCheckContext context,
     ScopeProperties scopeProperties)
     : base(context, scopeProperties)
 {
 }
Exemple #24
0
 public IdentityServer(HttpClient client, IConfiguration config, ScopeProperties scopeProperties) :
     base(client, config, scopeProperties)
 {
 }
Exemple #25
0
 public SyncExecutionRepo(EmmaContext context, ScopeProperties scopeProperties)
     : base(context, scopeProperties)
 {
 }
Exemple #26
0
 /// <summary>
 /// Constructs a new RepoBase object using the provided DbContext
 /// </summary>
 /// <param name="context">Entity Framework DbContext</param>
 public WriteableTemporalRepo(TContext context, THistoryContext historyContext,
                              ScopeProperties scopeProperties) : base(context, scopeProperties)
 {
     HistoryContext = historyContext;
 }
 public RgbRepo(ColorsDbContext context, ScopeProperties scopeProperties)
     : base(context, scopeProperties)
 {
 }
Exemple #28
0
 /// <summary>
 /// Constructs a new RepoBase object using the provided DbContext
 /// </summary>
 /// <param name="context">Entity Framework DbContext</param>
 public WriteableRepo(TContext context, ScopeProperties scopeProperties)
 {
     Context         = context;
     ScopeProperties = scopeProperties;
 }
 public ColorRepo(ColorDbContext context, ColorHistoryDbContext historyContext, ScopeProperties scopeProperties)
     : base(context, historyContext, scopeProperties)
 {
 }
Exemple #30
0
 public MessageRepo(EmmaContext context, EmmaHistoryContext historyContext, ScopeProperties scopeProperties)
     : base(context, historyContext, scopeProperties)
 {
 }