/// <summary> /// Create an <see cref="ILoggerProvider"/> for Google Cloud Logging. /// </summary> public static GoogleLoggerProvider Create(IServiceProvider serviceProvider, LoggingServiceOptions options = null) { options = options ?? new LoggingServiceOptions(); var loggingOptions = options.Options ?? LoggingOptions.Create(); var logTarget = options.LogTarget ?? LogTarget.ForProject(Project.GetAndCheckProjectId(null, loggingOptions.MonitoredResource)); var serviceContext = ServiceContextUtils.CreateServiceContext( Project.GetServiceName(options.ServiceName, loggingOptions.MonitoredResource), Project.GetServiceName(options.Version, loggingOptions.MonitoredResource)); var client = options.Client ?? LoggingServiceV2Client.Create(); IConsumer <LogEntry> consumer = LogConsumer.Create(client, loggingOptions.BufferOptions, loggingOptions.RetryOptions); var provider = new GoogleLoggerProvider(consumer, logTarget, serviceContext, loggingOptions, serviceProvider); var writer = options.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)provider.CreateLogger("SampleLogName")).WriteDiagnostics(writer); } return(provider); }
/// <summary> /// Configures Google Diagnostics to be used in non ASP.NET Core applications. /// </summary> /// <remarks> /// Options may be null in which case defaults will be used. Note that the /// Google Cloud Project ID to use is required. If not set via options, it will be /// obtained from the environment, but only if running on GCP. /// </remarks> public static IServiceCollection AddGoogleDiagnostics( this IServiceCollection services, TraceServiceOptions traceOptions = null, LoggingServiceOptions loggingOptions = null, ErrorReportingServiceOptions errorReportingOptions = null) => services .AddGoogleTrace(traceOptions) .AddLogging(builder => builder.AddGoogle(loggingOptions)) .AddGoogleErrorReporting(errorReportingOptions);
/// <summary> /// Adds a <see cref="GoogleLoggerProvider"/> for <see cref="GoogleLogger"/>s. /// </summary> public static ILoggingBuilder AddGoogle(this ILoggingBuilder builder, LoggingServiceOptions options = null) { builder.Services.AddSingleton <ILoggerProvider, GoogleLoggerProvider>(sp => GoogleLoggerProvider.Create(sp, options)); return(builder); }