Rollbar.NET icon indicating copy to clipboard operation
Rollbar.NET copied to clipboard

Rollbar.PlugIns.Log4Net on dotNetCore 2.0 project - rollbar settings from appsettings.<environment>.json are ignored

Open jensProTeria opened this issue 4 years ago • 13 comments

Using Rollbar.PlugIns.Log4Net (v3.5.0) on dotNetCore 2.0 mvc web project - all rollbar settings defined in appsettings..json are ignored - its always used appSettings.json no matter what enviroment in the dotNetCore Start up is defined.

I did following logs in Configure method in Startup.cs: public void Configure(IApplicationBuilder app, IHostingEnvironment env, IApplicationLifetime applicationLifetime, ILoggerFactory loggerFactory) { log.Warn("Starting in EnvironmentSet: " + env.EnvironmentName + " -- IsDevelopment: " + env.IsDevelopment().ToString()); var val = Configuration.GetSection("Rollbar:Environment"); log.Warn("Config for Rollbar Environment is: "+ val.Value); ... } All logged values are showing correct values from my set environment "MyTestServer" from appSettings.MyTestServer.json, wher I have set Rollbar environment to "MyTestServer" and logLevel "warn". Nevertheless on rollbar.com I get all logs with "debug" environment and logLevel "debug" like it is set in appsettings.json.

I so changed values for Rollbar logLevel and environment in appSettings.json on my server, after restarting the app, these new settings are used to push up issues to rollbar.

So the ConfigurationSet is loaded correctly, since Configuration.GetSection("..") gets correct values from appSettings..json but somehow Rollbar settings are always loaded from appsettings.json.

From Program.cs:

WebHost.CreateDefaultBuilder(args) .UseStartup<Startup>() .ConfigureAppConfiguration((hostContext, config) => { string envName = hostContext.HostingEnvironment.EnvironmentName; config.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true); config.AddJsonFile($"appsettings.{envName}.json", optional: true); }) .CaptureStartupErrors(true) .Build();

Rollbar Client - Rollbar.NET Hosting Environment (please complete the following information):

  • Rollbar.NET Host type: [web service, Windows service/daemon, desktop app, mobile app, etc.]
  • OS and its version: Windows Server 2012
  • .NET flavor and its version: .NET Core 2.0
  • Application framework and its version: [Asp.NET mvc
  • Rollbar.NET Version: [e.g. 3.5.0]
  • Applicable Rollbar Plug-in (if any): [Rollbar.PlugIns.Log4net]
  • Applicable custom third party logging library/framework integration Log4net
  • Applicable Browser (if any): [e.g. chrome, safari]

jensProTeria avatar Oct 28 '19 10:10 jensProTeria

@jensProTeria, thanks for reporting it. Meanwhile, you can use

RollbarConfigUtility.LoadRollbarConfig(string configFileName, string configFilePath = null) to load the config object from any custom file/location and, then, to initialize/configure a RollbarLogger with it.

akornich avatar Nov 01 '19 19:11 akornich

Same issue. Was using :

private void ConfigureRollbarSingleton()
        {
            string rollbarEnabled = Configuration.GetSection("Rollbar:Enabled").Value;
            if (rollbarEnabled?.ToLowerInvariant() != "false")
            {
                string rollbarAccessToken = Configuration.GetSection("Rollbar:AccessToken").Value;
                string rollbarEnvironment = Configuration.GetSection("Rollbar:Environment").Value;

                RollbarLocator.RollbarInstance
                    // minimally required Rollbar configuration:
                    // if you remove line below the logger's configuration will be auto-loaded from appsettings.json
                    .Configure(new RollbarConfig(rollbarAccessToken) { Environment = rollbarEnvironment })
                    // optional step if you would like to monitor Rollbar internal events within your application:
                    .InternalEvent += OnRollbarInternalEvent;
            }
        }

and surprise. it is using only the appsettings.json.

Could you add it in DOCS ? and other surprises? Thanks.

Lechus avatar Nov 04 '19 13:11 Lechus

@jensProTeria, thanks for reporting it. Meanwhile, you can use

RollbarConfigUtility.LoadRollbarConfig(string configFileName, string configFilePath = null) to load the config object from any custom file/location and, then, to initialize/configure a RollbarLogger with it.

RollbarConfigUtility.LoadRollbarConfig() has no overload taking 2 arguments ??!! #region Assembly Rollbar, Version=3.5.0.0, Culture=neutral, PublicKeyToken=null // C:.....nuget\packages\rollbar\3.5.0\lib\net461\Rollbar.dll #endregion public static class RollbarConfigUtility { ... public static IRollbarConfig LoadRollbarConfig(); .. }

jensProTeria avatar Nov 04 '19 13:11 jensProTeria

@jensProTeria // Assembly location: D:\NugetCache\rollbar\3.5.0\lib\netcoreapp2.1\Rollbar.dll

/// <summary>Loads the rollbar configuration.</summary> /// <param name="configFileName">Name of the configuration file.</param> /// <param name="configFilePath">The configuration file path.</param> /// <returns>Either IRollbarConfig or null if no configuration file found.</returns> public static IRollbarConfig LoadRollbarConfig( string configFileName, string configFilePath = null) { RollbarConfig config = new RollbarConfig("seedToken"); if (string.IsNullOrWhiteSpace(configFilePath)) { if (!AppSettingsUtility.LoadAppSettings(config, configFileName)) return (IRollbarConfig) null; } else if (!AppSettingsUtility.LoadAppSettings(config, configFilePath, configFileName)) return (IRollbarConfig) null; return (IRollbarConfig) config; }

Lechus avatar Nov 04 '19 13:11 Lechus

@akornich : there is no transformations in Core, you should not hardcode 'appsettings.json'.

Lechus avatar Nov 04 '19 13:11 Lechus

this ctor is sufficient , but how to set up log4net(?)/ Rollbar to use it? public RollbarAppender( string rollbarAccessToken, string rollbarEnvironment, TimeSpan? rollbarBlockingLoggingTimeout )

Lechus avatar Nov 04 '19 15:11 Lechus

grabbed code from Log4NET appender project (to not use nuget) and still, although passing config with access token, Rollbar code is searching for settings (like in appsettings.json) ?

There are so many lines of code that the Rollbar is created without config reference - then it reload config, but still throwing an error: Exception thrown: 'System.ArgumentException' in Rollbar.dll Exception thrown: 'System.Reflection.TargetInvocationException' in System.Private.CoreLib.dll log4net:ERROR Could not create Appender [RollbarAppender] of type [Rollbar.PlugIns.Log4net.RollbarAppender, Rollbar.PlugIns.Log4net]. Reported error follows. System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.ArgumentException: Parameter should not be null or white space. Parameter name: accessToken at Rollbar.Diagnostics.Assumption.FailValidation(String msg, String argumentName) at Rollbar.Diagnostics.Assumption.AssertNotNullOrWhiteSpace(String value, String parameterName) at Rollbar.AccessTokenQueuesMetadata..ctor(String accessToken) at Rollbar.RollbarQueueController.IndexByToken(PayloadQueue queue) at Rollbar.RollbarQueueController.Register(PayloadQueue queue) at Rollbar.RollbarLogger..ctor(Boolean isSingleton, IRollbarConfig rollbarConfig) at Rollbar.RollbarFactory.CreateNew(Boolean isSingleton, IRollbarConfig rollbarConfig) at Rollbar.RollbarFactory.CreateNew(IRollbarConfig rollbarConfig) at Rollbar.RollbarFactory.CreateNew() at Rollbar.PlugIns.PlugInCoreBase..ctor(IRollbarConfig rollbarConfig, Nullable1 rollbarBlockingTimeout) at Rollbar.PlugIns.PlugInCore2..ctor(IDictionary2 rollbarErrorLevelByPlugInErrorLevel, String customPrefix, IRollbarConfig rollbarConfig, Nullable1 rollbarBlockingTimeout) at Rollbar.PlugIns.Log4net.RollbarPlugInCore..ctor(IRollbarConfig rollbarConfig, Nullable1 rollbarBlockingTimeout) in E:\Projects\fluencyms\Webhooks\Rollbar.PlugIns.Log4net\RollbarPlugInCore.cs:line 86 at Rollbar.PlugIns.Log4net.RollbarAppender..ctor(IRollbarConfig rollbarConfig, Nullable1 rollbarBlockingLoggingTimeout) in E:\Projects\fluencyms\Webhooks\Rollbar.PlugIns.Log4net\RollbarAppender.cs:line 65 at Rollbar.PlugIns.Log4net.RollbarAppender..ctor() in E:\Projects\fluencyms\Webhooks\Rollbar.PlugIns.Log4net\RollbarAppender.cs:line 24 --- End of inner exception stack trace --- at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean wrapExceptions, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor) at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean wrapExceptions, Boolean skipCheckThis, Boolean fillCache) at System.Activator.CreateInstance(Type type, Boolean nonPublic, Boolean wrapExceptions) at log4net.Repository.Hierarchy.XmlHierarchyConfigurator.ParseAppender(XmlElement appenderElement) log4net:ERROR Appender named [RollbarAppender] not found.

Lechus avatar Nov 05 '19 12:11 Lechus

@jensProTeria , RollbarConfig's static method public static IRollbarConfig LoadRollbarConfig(string configFileName, string configFilePath = null) if conditionally compiled for .NET Core only. As of now, there is no equivalent available for .NET Framework-based application. In your initial post you mentioned you are using Rollbar.NET SDK (via its Rollbar.PlugIns.Log4Net) within .NET Core 2.0? So, the method should be available to you...

akornich avatar Nov 07 '19 21:11 akornich

@Lechus , I am not sure I am following what problem you are running into. May I ask you to elaborate a bit more on it? If it is not close enough to the @jensProTeria 's issue, may I ask you to open a new GitHub issue for it?

akornich avatar Nov 07 '19 21:11 akornich

In general, we are not taking into account Asp.Net Core's different runtime environments within the SDK. The primary reason is that the SDK targets different types of applications, application frameworks, .NET flavors, etc. However, I believe we do provide enough flexibility and convenience API for more general use and for some specific frameworks to make the adoption possible and easy for a wide variety of scenarios. And, we are always open to making it better and easier to use. So, if you see we missed something or there are ways to improve the SDK let us know.

akornich avatar Nov 07 '19 21:11 akornich

@akornich in microservies world - forcing users to use appsettings.json with credentials/tokens is not good. Maybe you could review AWS SDK how it grabs credentials? In short , it reads them from ENVIRONMENT too.

I could not find how to pass accesstoken to rollbar that is under log4net in other way than just use appsettings.json or app.config.

Lechus avatar Nov 15 '19 11:11 Lechus

same situation with Rollbar.PlugIns.NLog

i was strugling to find out why is not working with .net core 2.2. Reason is Rollbar doesn't read appsettings.environment.json

andrzejwidera avatar Dec 05 '19 21:12 andrzejwidera

@andrzejwidera , correct, at the moment we only automatically support appsettings.json, app.config, and web.config. OR any custom config file by explicitly loading RollbarConfig from its content with the help of RollbarConfigUtility class.

akornich avatar Dec 05 '19 22:12 akornich