Skip to content

Commit 31e4e8b

Browse files
committed
When reading the config for creating the installation do it with file read over using IConfiguration which may throw an exception
1 parent e07487b commit 31e4e8b

File tree

1 file changed

+12
-14
lines changed

1 file changed

+12
-14
lines changed

src/Elmah.Io.AspNetCore/MessageShipper.cs

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.IO;
34
using System.Linq;
45
using System.Net.Http.Headers;
56
using System.Text;
@@ -9,10 +10,9 @@
910
using Microsoft.AspNetCore.Builder;
1011
using Microsoft.AspNetCore.Http;
1112
using Microsoft.AspNetCore.Http.Features;
12-
using Microsoft.Extensions.Configuration;
1313
using Microsoft.Extensions.DependencyInjection;
1414
using Microsoft.Extensions.Options;
15-
using Newtonsoft.Json;
15+
using Newtonsoft.Json.Linq;
1616

1717
namespace Elmah.Io.AspNetCore
1818
{
@@ -115,25 +115,23 @@ public static void CreateInstallation(IApplicationBuilder app)
115115
Loggers = [logger]
116116
};
117117

118-
try
118+
var location = typeof(MessageShipper).Assembly.Location;
119+
var currentDirectory = Path.GetDirectoryName(location);
120+
var appsettingsFilePath = Path.Combine(currentDirectory, "appsettings.json");
121+
if (File.Exists(appsettingsFilePath))
119122
{
120-
var configuration = app.ApplicationServices.GetService<IConfiguration>();
121-
var elmahio = configuration.GetSection("ElmahIo").Get<ElmahIoOptions>();
122-
if (elmahio != null)
123+
var appsettingsContent = File.ReadAllText(appsettingsFilePath);
124+
var appsettingsObject = JObject.Parse(appsettingsContent);
125+
if (appsettingsObject.TryGetValue("ElmahIo", out JToken elmahIoSection))
123126
{
124127
logger.ConfigFiles.Add(new ConfigFile
125128
{
126-
Name = "appsettings.json",
127-
Content = JsonConvert.SerializeObject(elmahio),
128-
ContentType = "application/json",
129+
Name = Path.GetFileName(appsettingsFilePath),
130+
Content = new JObject { { "ElmahIo", elmahIoSection.DeepClone() } }.ToString(),
131+
ContentType = "application/json"
129132
});
130133
}
131134
}
132-
catch
133-
{
134-
// There might be a problem with the config. Since we still reached this line the application
135-
// seem to start up. So, let us create the installation without the config file.
136-
}
137135

138136
var elmahioApi = ElmahioAPI.Create(options.ApiKey, new Client.ElmahIoOptions
139137
{

0 commit comments

Comments
 (0)