|
1 | 1 | using System; |
2 | 2 | using System.Collections.Generic; |
| 3 | +using System.IO; |
3 | 4 | using System.Linq; |
4 | 5 | using System.Net.Http.Headers; |
5 | 6 | using System.Text; |
|
9 | 10 | using Microsoft.AspNetCore.Builder; |
10 | 11 | using Microsoft.AspNetCore.Http; |
11 | 12 | using Microsoft.AspNetCore.Http.Features; |
12 | | -using Microsoft.Extensions.Configuration; |
13 | 13 | using Microsoft.Extensions.DependencyInjection; |
14 | 14 | using Microsoft.Extensions.Options; |
15 | | -using Newtonsoft.Json; |
| 15 | +using Newtonsoft.Json.Linq; |
16 | 16 |
|
17 | 17 | namespace Elmah.Io.AspNetCore |
18 | 18 | { |
@@ -115,25 +115,23 @@ public static void CreateInstallation(IApplicationBuilder app) |
115 | 115 | Loggers = [logger] |
116 | 116 | }; |
117 | 117 |
|
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)) |
119 | 122 | { |
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)) |
123 | 126 | { |
124 | 127 | logger.ConfigFiles.Add(new ConfigFile |
125 | 128 | { |
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" |
129 | 132 | }); |
130 | 133 | } |
131 | 134 | } |
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 | | - } |
137 | 135 |
|
138 | 136 | var elmahioApi = ElmahioAPI.Create(options.ApiKey, new Client.ElmahIoOptions |
139 | 137 | { |
|
0 commit comments