/// <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));
        }
Пример #2
0
 internal GoogleLogger(IConsumer <LogEntry> consumer, LogTarget logTarget, LoggerOptions loggerOptions, string logName,
                       Action <IServiceProvider, Dictionary <string, string> > obsoleteLabelsGetter,
                       Action <IServiceProvider, LogEntry, TraceTarget> obsoleteTraceContextGetter,
                       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;
     _obsoleteTraceContextGetter = obsoleteTraceContextGetter;
     _ambientScopeManager        = new AmbientScopeManager(_loggerOptions, _serviceProvider, obsoleteLabelsGetter);
 }
        /// <summary>
        /// <see cref="ILoggerProvider"/> for Google Cloud 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);
            }
        }
Пример #4
0
 internal AmbientScopeManager(LoggerOptions options, IServiceProvider serviceProvider, Action <IServiceProvider, Dictionary <string, string> > obsoleteLabelsGetter)
 {
     _permanentParent      = options?.Labels is null ? null : GoogleLoggerScope.CreateScope(new LabellingScopeState(options.Labels), null);
     _serviceProvider      = serviceProvider;
     _obsoleteLabelsGetter = obsoleteLabelsGetter;
 }