예제 #1
0
        /// <summary>
        /// Adds a target for Sentry to the NLog configuration.
        /// </summary>
        /// <param name="configuration">The NLog configuration.</param>
        /// <param name="dsn">          The sentry DSN.</param>
        /// <param name="targetName">   The name to give the new target.</param>
        /// <param name="optionsConfig">An optional action for configuring the Sentry target options.</param>
        /// <returns>The configuration.</returns>
        public static LoggingConfiguration AddSentry(this LoggingConfiguration configuration,
                                                     string dsn,
                                                     string targetName,
                                                     Action <SentryNLogOptions> optionsConfig = null)
        {
            var options = new SentryNLogOptions();

            optionsConfig?.Invoke(options);

            Target.Register <SentryTarget>("Sentry");

            var target = new SentryTarget(options)
            {
                Name   = targetName,
                Layout = "${message}",
            };

            if (dsn != null && options.Dsn == null)
            {
                options.Dsn = new Dsn(dsn);
            }

            configuration?.AddTarget(targetName, target);

            configuration?.AddRuleForAllLevels(targetName);

            return(configuration);
        }
        /// <summary>
        /// Adds a target for Sentry to the NLog configuration.
        /// </summary>
        /// <param name="configuration">The NLog configuration.</param>
        /// <param name="dsn">          The sentry DSN.</param>
        /// <param name="targetName">   The name to give the new target.</param>
        /// <param name="optionsConfig">An optional action for configuring the Sentry target options.</param>
        /// <returns>The configuration.</returns>
        public static LoggingConfiguration AddSentry(this LoggingConfiguration configuration,
                                                     string?dsn,
                                                     string targetName,
                                                     Action <SentryNLogOptions>?optionsConfig = null)
        {
            // Not to throw on code that ignores nullability warnings.
            // ReSharper disable once ConditionIsAlwaysTrueOrFalse
            if (configuration is null)
            {
                return(configuration !);
            }

            var options = new SentryNLogOptions();

            optionsConfig?.Invoke(options);

            Target.Register <SentryTarget>("Sentry");

            var target = new SentryTarget(options)
            {
                Name   = targetName,
                Layout = "${message}",
            };

            if (dsn != null && string.IsNullOrWhiteSpace(options.Dsn))
            {
                options.Dsn = dsn;
            }

            configuration.AddTarget(targetName, target);

            configuration.AddRuleForAllLevels(targetName);

            return(configuration);
        }