/// <summary>
 /// Create an <see cref="ILoggerProvider"/> for Google Cloud Logging.
 /// </summary>
 /// <param name="serviceProvider">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="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(IServiceProvider serviceProvider, string projectId = null,
                                           LoggerOptions options = null, LoggingServiceV2Client client = null)
 {
     options   = options ?? LoggerOptions.Create();
     projectId = Project.GetAndCheckProjectId(projectId, options.MonitoredResource);
     return(Create(LogTarget.ForProject(projectId), serviceProvider, options, client));
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Creates a new <see cref="EventTarget"/> instance that will report to the Stackdriver Logging API.
        /// The events are then automatically propigated to the Stackdriver Error Logging API from the
        /// Stackdriver Logging API.
        /// </summary>
        /// <remarks>
        /// For more information see "Formatting Log Error Messages"
        /// (https://cloud.google.com/error-reporting/docs/formatting-error-messages).
        /// </remarks>
        /// <param name="projectId">The Google Cloud Platform project Id. Cannot be null.</param>
        /// <param name="logName">The log name.  Cannot be null.</param>
        /// <param name="loggingClient">The logging client.</param>
        /// <param name="monitoredResource">Optional, the monitored resource.  The monitored resource will
        ///     be automatically detected if it is not set and will default to the global resource if the detection fails.
        ///     See: https://cloud.google.com/logging/docs/api/v2/resource-list </param>
        public static EventTarget ForLogging(string projectId, string logName     = LogNameDefault,
                                             LoggingServiceV2Client loggingClient = null, MonitoredResource monitoredResource = null)
        {
            var logTarget = LogTarget.ForProject(GaxPreconditions.CheckNotNull(projectId, nameof(projectId)));

            return(ForLogging(logTarget, logName, loggingClient, monitoredResource));
        }
        /// <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);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Creates a new <see cref="EventTarget"/> instance that will report to the Stackdriver Logging API.
        /// The events are then automatically propigated to the Stackdriver Error Logging API from the
        /// Stackdriver Logging API.
        /// </summary>
        /// <remarks>
        /// For more information see "Formatting Log Error Messages"
        /// (https://cloud.google.com/error-reporting/docs/formatting-error-messages).
        /// </remarks>
        /// <param name="projectId">Optional if running on Google App Engine or Google Compute Engine.
        ///     The Google Cloud Platform project ID. If running on GAE or GCE the project ID will be
        ///     detected from the platform.</param>
        /// <param name="logName">The log name.  Cannot be null.</param>
        /// <param name="loggingClient">The logging client.</param>
        /// <param name="monitoredResource">Optional, the monitored resource.  The monitored resource will
        ///     be automatically detected if it is not set and will default to the global resource if the detection fails.
        ///     See: https://cloud.google.com/logging/docs/api/v2/resource-list </param>
        public static EventTarget ForLogging(string projectId = null, string logName = LogNameDefault,
                                             LoggingServiceV2Client loggingClient = null, MonitoredResource monitoredResource = null)
        {
            projectId = CommonUtils.GetAndCheckProjectId(projectId, monitoredResource);
            var logTarget = LogTarget.ForProject(projectId);

            return(ForLogging(logTarget, logName, loggingClient, monitoredResource));
        }