/// <summary>
 /// Configures Google Diagnostics services for Logging, Tracing and Error Reporting middleware.
 /// </summary>
 /// <param name="builder">The <see cref="IWebHostBuilder"/> instance.</param>
 /// <param name="projectId">
 /// The Google Cloud Platform project ID. If unspecified and running on GAE/GCE/GKE
 /// the project ID will be detected from the platform.
 /// </param>
 /// <param name="serviceName">
 /// An identifier of the service used for exception logging, such as the name of the executable or job.
 /// If unspecified and running on GAE the service name will be detected from the platform.
 /// </param>
 /// <param name="serviceVersion">
 /// A string that represents the version of the service or the source code used for exception logging.
 /// If unspecified and running on GAE the service version will be detected from the platform.
 /// </param>
 /// <param name="loggerOptions">The options for logging. May be null, in which case default options will be used.</param>
 /// <param name="traceOptions">The options for tracing. May be null, in which case default options will be used.</param>
 /// <param name="errorReportingOptions">The options for error reporting. May be null, in which case default options will be used.</param>
 /// <returns>The <see cref="IWebHostBuilder"/> instance.</returns>
 public static IWebHostBuilder UseGoogleDiagnostics(
     this IWebHostBuilder builder,
     string projectId            = null,
     string serviceName          = null,
     string serviceVersion       = null,
     LoggerOptions loggerOptions = null,
     TraceOptions traceOptions   = null,
     ErrorReportingOptions errorReportingOptions = null) =>
 builder.ConfigureServices(services =>
                           ConfigureGoogleDiagnosticsServices(services, projectId, serviceName, serviceVersion, loggerOptions, traceOptions, errorReportingOptions));
Exemple #2
0
        /// <summary>
        /// Registers the startup filter configuration action.
        /// </summary>
        /// <param name="next">A callback to the next startup filter configuration.</param>
        /// <returns>A callback of the startup filter configuration.</returns>
        public Action <IApplicationBuilder> Configure(Action <IApplicationBuilder> next)
        {
            return(app =>
            {
                var loggerFactory = app.ApplicationServices.GetServiceCheckNotNull <ILoggerFactory>();
                loggerFactory.AddGoogle(app.ApplicationServices, _projectId, LoggerOptions.Create(monitoredResource: _monitoredResource));
                app.UseGoogleExceptionLogging();
                app.UseGoogleTrace();

                next(app);
            });
        }
Exemple #3
0
 internal GoogleLogger(IConsumer <LogEntry> consumer, LogTarget logTarget, LoggerOptions loggerOptions,
                       string logName, IClock clock = null, IHttpContextAccessor accessor = null)
 {
     GaxPreconditions.CheckNotNull(logTarget, nameof(logTarget));
     GaxPreconditions.CheckNotNullOrEmpty(logName, nameof(logName));
     _traceTarget = logTarget.Kind == LogTargetKind.Project ?
                    TraceTarget.ForProject(logTarget.ProjectId) : null;
     _consumer      = GaxPreconditions.CheckNotNull(consumer, nameof(consumer));
     _loggerOptions = GaxPreconditions.CheckNotNull(loggerOptions, nameof(loggerOptions));;
     _logName       = logTarget.GetFullLogName(logName);
     _accessor      = accessor;
     _clock         = clock ?? SystemClock.Instance;
 }
Exemple #4
0
 internal GoogleLogger(IConsumer <LogEntry> consumer, LogTarget logTarget, LoggerOptions loggerOptions,
                       string logName, IClock clock = null, IServiceProvider serviceProvider = null)
 {
     _logTarget   = GaxPreconditions.CheckNotNull(logTarget, nameof(logTarget));
     _traceTarget = logTarget.Kind == LogTargetKind.Project ?
                    TraceTarget.ForProject(logTarget.ProjectId) : null;
     _consumer        = GaxPreconditions.CheckNotNull(consumer, nameof(consumer));
     _loggerOptions   = GaxPreconditions.CheckNotNull(loggerOptions, nameof(loggerOptions));
     _logName         = GaxPreconditions.CheckNotNullOrEmpty(logName, nameof(logName));
     _fullLogName     = logTarget.GetFullLogName(_loggerOptions.LogName);
     _serviceProvider = serviceProvider;
     _clock           = clock ?? SystemClock.Instance;
 }
Exemple #5
0
        /// <summary>
        /// Create an <see cref="ILoggerProvider"/> for Google Stackdriver Logging.
        /// </summary>
        /// <param name="logTarget">Where to log to. Cannot be null.</param>
        /// <param name="options">Optional, options for the logger.</param>
        /// <param name="client">Optional, logging client.</param>
        public static GoogleLoggerProvider Create(LogTarget logTarget,
                                                  LoggerOptions options = null, LoggingServiceV2Client client = null)
        {
            // Check params and set defaults if unset.
            GaxPreconditions.CheckNotNull(logTarget, nameof(logTarget));
            client  = client ?? LoggingServiceV2Client.Create();
            options = options ?? LoggerOptions.Create();

            // Get the proper consumer from the options and add a logger provider.
            IConsumer <LogEntry> consumer = LogConsumer.Create(client, options.BufferOptions, options.RetryOptions);

            return(new GoogleLoggerProvider(consumer, logTarget, options));
        }
        /// <summary>
        /// <see cref="ILoggerProvider"/> for Google Stackdriver Logging.
        /// </summary>
        /// <param name="consumer">The consumer to push logs to. Must not be null.</param>
        /// <param name="logTarget">Where to log to. Must not be null.</param>
        /// <param name="loggerOptions">The logger options. Must not be null.</param>
        /// <param name="serviceProvider">The service provider to resolve additional services from.</param>
        internal GoogleLoggerProvider(IConsumer <LogEntry> consumer, LogTarget logTarget, LoggerOptions loggerOptions, IServiceProvider serviceProvider)
        {
            _consumer        = GaxPreconditions.CheckNotNull(consumer, nameof(consumer));
            _logTarget       = GaxPreconditions.CheckNotNull(logTarget, nameof(logTarget));
            _loggerOptions   = GaxPreconditions.CheckNotNull(loggerOptions, nameof(loggerOptions));
            _serviceProvider = serviceProvider;

            var writer = loggerOptions.LoggerDiagnosticsOutput;

            if (writer != null)
            {
                // The log name is the ASP.NET Core log name, not the "/projects/xyz/logs/abc" log name in the resource.
                // We don't currently use this in the diagnostics, but if we ever start to do so, SampleLogName seems
                // like a reasonably clear example.
                ((GoogleLogger)CreateLogger("SampleLogName")).WriteDiagnostics(writer);
            }
        }
        /// <summary>
        /// Adds a <see cref="GoogleLoggerProvider"/> for <see cref="GoogleLogger"/>s.
        /// </summary>
        /// <param name="factory">The logger factory. Cannot be null.</param>
        /// <param name="logTo">Where to log to. Cannot be null. Cannot be null.</param>
        /// <param name="options">Optional, options for the logger.</param>
        /// <param name="client">Optional, logging client.</param>
        public static ILoggerFactory AddGoogle(this ILoggerFactory factory, LogTo logTo,
                                               LoggerOptions options = null, LoggingServiceV2Client client = null)
        {
            // Check params and set defaults if unset.
            GaxPreconditions.CheckNotNull(factory, nameof(factory));
            GaxPreconditions.CheckNotNull(logTo, nameof(logTo));
            client  = client ?? LoggingServiceV2Client.Create();
            options = options ?? LoggerOptions.Create();

            // Get the proper consumer from the options and add a logger provider.
            GrpcLogConsumer      grpcConsumer = new GrpcLogConsumer(client);
            IConsumer <LogEntry> consumer     = ConsumerFactory <LogEntry> .GetConsumer(
                grpcConsumer, LogEntrySizer.Instance, options.BufferOptions);

            GoogleLoggerProvider provider = new GoogleLoggerProvider(consumer, logTo, options);

            factory.AddProvider(provider);
            return(factory);
        }
 /// <summary>
 /// Adds a <see cref="GoogleLoggerProvider"/> for <see cref="GoogleLogger"/>s.
 /// </summary>
 /// <param name="factory">The logger factory. Cannot be null.</param>
 /// <param name="projectId">The Google Cloud Platform project ID. Cannot be null.</param>
 /// <param name="options">Optional, options for the logger.</param>
 /// <param name="client">Optional, logging client.</param>
 public static ILoggerFactory AddGoogle(this ILoggerFactory factory, string projectId,
                                        LoggerOptions options = null, LoggingServiceV2Client client = null)
 {
     GaxPreconditions.CheckNotNull(projectId, nameof(projectId));
     return(factory.AddGoogle(LogTo.Project(projectId), options, client));
 }
Exemple #9
0
 /// <summary>
 /// <see cref="ILoggerProvider"/> for Google Stackdriver Logging.
 /// </summary>
 /// <param name="consumer">The consumer to push logs to. Cannot be null.</param>
 /// <param name="logTarget">Where to log to. Cannot be null.</param>
 /// <param name="loggerOptions">The logger options. Cannot be null.</param>
 internal GoogleLoggerProvider(IConsumer <LogEntry> consumer, LogTarget logTarget, LoggerOptions loggerOptions)
 {
     _consumer      = GaxPreconditions.CheckNotNull(consumer, nameof(consumer));
     _logTarget     = GaxPreconditions.CheckNotNull(logTarget, nameof(logTarget));
     _loggerOptions = GaxPreconditions.CheckNotNull(loggerOptions, nameof(loggerOptions));
 }
Exemple #10
0
 /// <summary>
 /// Initializes a new instance of the <see cref="GoogleDiagnosticsStartupFilter"/> class to configure Google Diagnostics services.
 /// </summary>
 /// <param name="projectId">
 /// The Google Cloud Platform project ID. If unspecified and running on GAE or GCE
 /// the project ID will be detected from the platform.
 /// </param>
 /// <param name="loggerOptions">The logger options. May be null.</param>
 public GoogleDiagnosticsStartupFilter(string projectId, LoggerOptions loggerOptions)
 {
     _projectId     = projectId;
     _loggerOptions = loggerOptions;
 }
Exemple #11
0
 /// <summary>
 /// Create an <see cref="ILoggerProvider"/> for Google Stackdriver Logging.
 /// </summary>
 /// <param name="projectId">Optional if running on Google App Engine or Google Compute Engine.
 ///     The Google Cloud Platform project ID. If unspecified and running on GAE or GCE the project ID will be
 ///     detected from the platform.</param>
 /// <param name="options">Optional, options for the logger.</param>
 /// <param name="client">Optional, logging client.</param>
 public static GoogleLoggerProvider Create(string projectId,
                                           LoggerOptions options = null, LoggingServiceV2Client client = null)
 {
     projectId = Project.GetAndCheckProjectId(projectId, options.MonitoredResource);
     return(Create(LogTarget.ForProject(projectId), options, client));
 }