示例#1
0
 public static IHostBuilder CreateHostBuilder() =>
 new HostBuilder()
 .ConfigureLogging(builder => builder.ClearProviders())
 .ConfigureServices(services =>
 {
     // We don't care much about which options we are setting.
     // These are for testing that all the extension method overloads work as expected.
     services.AddGoogleDiagnostics(
         new TraceServiceOptions
     {
         ProjectId = ProjectId,
         Options   = TraceOptions.Create(retryOptions: RetryOptions.NoRetry(ExceptionHandling.Propagate))
     },
         new LoggingServiceOptions
     {
         ProjectId   = ProjectId,
         ServiceName = Service,
         Version     = Version,
         Options     = LoggingOptions.Create(retryOptions: RetryOptions.NoRetry(ExceptionHandling.Propagate))
     },
         new ErrorReportingServiceOptions
     {
         ProjectId   = ProjectId,
         ServiceName = Service,
         Version     = Version,
         Options     = ErrorReportingOptions.CreateInstance(retryOptions: RetryOptions.NoRetry(ExceptionHandling.Propagate))
     });
 });
 // Sample: ConfigureBuffers
 public static IHostBuilder CreateHostBuilder() =>
 new HostBuilder()
 .ConfigureServices(services =>
 {
     // Replace ProjectId with your Google Cloud Project ID.
     // Replace Service with a name or identifier for the service.
     // Replace Version with a version for the service.
     services.AddGoogleDiagnostics(ProjectId, Service, Version,
                                   // Configure the three components to use no buffer.
                                   traceOptions: TraceOptions.Create(bufferOptions: BufferOptions.NoBuffer()),
                                   loggingOptions: LoggingOptions.Create(bufferOptions: BufferOptions.NoBuffer()),
                                   errorReportingOptions: ErrorReportingOptions.CreateInstance(bufferOptions: BufferOptions.NoBuffer()));
     // Register other services here if you need them.
 });
示例#3
0
        private GoogleLogger GetLogger(
            IConsumer <LogEntry> consumer      = null, LogLevel logLevel = LogLevel.Information,
            Dictionary <string, string> labels = null, IServiceProvider serviceProvider = null,
            string logName = null,
            MonitoredResource monitoredResource = null, LogTarget logTarget = null,
            RetryOptions retryOptions           = null,
            string serviceName = null,
            string version     = null)
        {
            consumer ??= new Mock <IConsumer <LogEntry> >(MockBehavior.Strict).Object;
            monitoredResource ??= MonitoredResourceBuilder.GlobalResource;
            logTarget ??= s_defaultLogTarget;
            var options        = LoggingOptions.Create(logLevel, logName, labels, monitoredResource, retryOptions: retryOptions);
            var serviceContext = ServiceContextUtils.CreateServiceContext(serviceName, version);

            return(new GoogleLogger(consumer, logTarget, serviceContext, options, LogName, s_clock, serviceProvider));
        }
示例#4
0
        public async Task UseGoogleDiagnostics_ValidateDependencyInjection()
        {
            var testId    = IdGenerator.FromDateTime();
            var startTime = DateTime.UtcNow;

            var hostBuilder = GetHostBuilder(webHostBuilder =>
                                             webHostBuilder
                                             .UseDefaultServiceProvider(options => options.ValidateScopes = true)
                                             .ConfigureServices(services =>
                                                                services.AddGoogleDiagnosticsForAspNetCore(
                                                                    TestEnvironment.GetTestProjectId(), EntryData.Service, EntryData.Version,
                                                                    traceOptions: TraceOptions.Create(retryOptions: RetryOptions.NoRetry(ExceptionHandling.Propagate)),
                                                                    loggingOptions: LoggingOptions.Create(retryOptions: RetryOptions.NoRetry(ExceptionHandling.Propagate)),
                                                                    errorReportingOptions: ErrorReportingOptions.CreateInstance(retryOptions: RetryOptions.NoRetry(ExceptionHandling.Propagate)))));

            using var server = GetTestServer(hostBuilder);
            using var client = server.CreateClient();
            await TestTrace(testId, startTime, client);
            await TestLogging(testId, startTime, client);
            await TestErrorReporting(testId, client);
        }
            // Sample: PropagatesExceptions
            public static IHostBuilder CreateHostBuilder() =>
            new HostBuilder()
            .ConfigureServices(services =>
            {
                //Explicitly create logger options that will propagate any exceptions thrown
                // during logging.
                RetryOptions retryOptions = RetryOptions.NoRetry(ExceptionHandling.Propagate);
                // Also set the no buffer option so that writing the logs is attempted immediately.
                BufferOptions bufferOptions = BufferOptions.NoBuffer();

                // Replace ProjectId with your Google Cloud Project ID.
                services.AddLogging(builder => builder.AddGoogle(new LoggingServiceOptions
                {
                    // Replace ProjectId with your Google Cloud Project ID.
                    ProjectId = ProjectId,
                    // Replace Service with a name or identifier for the service.
                    ServiceName = Service,
                    // Replace Version with a version for the service.
                    Version = Version,
                    Options = LoggingOptions.Create(retryOptions: retryOptions, bufferOptions: bufferOptions)
                }));;
                // Register other services here if you need them.
            });