/// <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);
        }
Exemple #2
0
        internal static IContextExceptionLogger Create(ErrorReportingServiceOptions options, IServiceProvider serviceProvider)
        {
            options = options ?? new ErrorReportingServiceOptions();
            var errorReportingOptions = options.Options ?? ErrorReportingOptions.CreateInstance();

            var eventTarget = options.EventTarget
#pragma warning disable CS0618 // Type or member is obsolete
                              ?? errorReportingOptions.EventTarget
#pragma warning restore CS0618 // Type or member is obsolete
                              ?? EventTarget.ForProject(Project.GetAndCheckProjectId(null, errorReportingOptions.MonitoredResource));

            var client = options.Client
#pragma warning disable CS0618 // Type or member is obsolete
                         ?? eventTarget.LoggingClient
#pragma warning restore CS0618 // Type or member is obsolete
                         ?? LoggingServiceV2Client.Create();

            var serviceContext = CreateServiceContext(
                Project.GetServiceName(options.ServiceName, errorReportingOptions.MonitoredResource),
                Project.GetServiceName(options.Version, errorReportingOptions.MonitoredResource))
                                 ?? new Struct();

            IConsumer <LogEntry> consumer = LogConsumer.Create(client, errorReportingOptions.BufferOptions, errorReportingOptions.RetryOptions);

            return(new ErrorReportingContextExceptionLogger(consumer, eventTarget, serviceContext, errorReportingOptions, serviceProvider));
        }
        /// <summary>
        /// Create an <see cref="ILoggerProvider"/> for Google Cloud Logging.
        /// </summary>
        /// <param name="logTarget">Where to log to. Must not be null.</param>
        /// <param name="serviceProvider">Optional, the service provider to resolve additional services from. May be null,
        /// in which case additional services (such as custom labels) will not be used.</param>
        /// <param name="options">Optional, options for the logger.</param>
        /// <param name="client">Optional, logging client.</param>
        public static GoogleLoggerProvider Create(LogTarget logTarget, IServiceProvider serviceProvider,
                                                  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, serviceProvider));
        }