Skip to content

Commit 320016c

Browse files
author
marc
committed
Add support for reading Stackify.json
1 parent 9de8b81 commit 320016c

File tree

5 files changed

+79
-6
lines changed

5 files changed

+79
-6
lines changed

Src/StackifyLib/Config.cs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.IO;
34
using System.Linq;
5+
using Newtonsoft.Json.Linq;
46
using StackifyLib.Utils;
57

68
namespace StackifyLib
@@ -11,6 +13,8 @@ namespace StackifyLib
1113
/// </summary>
1214
public class Config
1315
{
16+
17+
private static readonly JsonLoadSettings Settings = new JsonLoadSettings { CommentHandling = CommentHandling.Ignore, LineInfoHandling = LineInfoHandling.Ignore };
1418
#if NETCORE || NETCOREX
1519

1620
private static Microsoft.Extensions.Configuration.IConfiguration _configuration = null;
@@ -197,5 +201,48 @@ internal static string Get(string key, string defaultValue = null)
197201

198202
return v;
199203
}
204+
205+
public static void ReadStackifyJSONConfig(string filePath)
206+
{
207+
string json;
208+
209+
using (var fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
210+
{
211+
using (var sr = new StreamReader(fs))
212+
{
213+
json = sr.ReadToEnd();
214+
}
215+
}
216+
217+
var obj = JObject.Parse(json, Settings);
218+
Config.SetStackifyObj(obj);
219+
}
220+
221+
public static void SetStackifyObj(JObject obj)
222+
{
223+
AppName = TryGetValue(obj, "AppName");
224+
Environment = TryGetValue(obj, "Environment");
225+
ApiKey = TryGetValue(obj, "ApiKey");
226+
}
227+
228+
private static string TryGetValue(JToken jToken, string key)
229+
{
230+
string r = null;
231+
232+
try
233+
{
234+
var val = jToken[key];
235+
if (val != null)
236+
{
237+
r = val.ToString();
238+
}
239+
}
240+
catch (Exception ex)
241+
{
242+
StackifyAPILogger.Log("#Config #TryGetValue failed", ex);
243+
}
244+
245+
return r;
246+
}
200247
}
201248
}

samples/CoreConsoleApp/Program.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,17 @@
99
using log4net.Config;
1010
using log4net.Repository;
1111
using Microsoft.Extensions.Configuration;
12+
using Newtonsoft.Json.Linq;
1213
using NLog;
1314
using NLog.Config;
1415
using StackifyLib;
16+
using static System.Net.WebRequestMethods;
1517

1618
namespace CoreConsoleApp
1719
{
1820
public class Program
1921
{
22+
private static readonly JsonLoadSettings Settings = new JsonLoadSettings { CommentHandling = CommentHandling.Ignore, LineInfoHandling = LineInfoHandling.Ignore };
2023
static void Main(string[] args)
2124
{
2225
var builder = new ConfigurationBuilder()
@@ -32,8 +35,15 @@ static void Main(string[] args)
3235
StackifyLib.Utils.StackifyAPILogger.OnLogMessage += StackifyAPILogger_OnLogMessage;
3336
StackifyLib.Utils.StackifyAPILogger.LogEnabled = true;
3437

35-
//NLogTest();
3638

39+
string filePath = "C:\\Source\\stackify-api-dotnet\\samples\\CoreConsoleApp\\Stackify.json";
40+
41+
Config.ReadStackifyJSONConfig(filePath);
42+
43+
//NLogTest();
44+
Console.WriteLine($"Stackify Config AppName: {StackifyLib.Config.AppName}");
45+
Console.WriteLine($"Stackify Config EnvironmentName: {StackifyLib.Config.Environment}");
46+
Console.WriteLine($"Stackify Config ApiKey: {StackifyLib.Config.ApiKey}");
3747
StackifyLib.Logger.Shutdown(); //best practice for console apps
3848
}
3949

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"AppName": "CoreConsoleApp",
3+
"Environment": "Dev",
4+
"ApiKey": "sampleKey"
5+
}

samples/CoreWebApp/Stackify.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"AppName": "CoreWebApp",
3+
"Environment": "Dev",
4+
"ApiKey": "sampleKey"
5+
}

samples/CoreWebApp/Startup.cs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,22 @@ public Startup(IHostingEnvironment env)
2929
StackifyLib.Utils.StackifyAPILogger.OnLogMessage += StackifyAPILogger_OnLogMessage;
3030
StackifyLib.Utils.StackifyAPILogger.LogEnabled = true;
3131

32-
var builder = new ConfigurationBuilder()
32+
/*var builder = new ConfigurationBuilder()
3333
.SetBasePath(env.ContentRootPath)
3434
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
3535
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
3636
.AddEnvironmentVariables();
3737
Configuration = builder.Build();
3838
Configuration.ConfigureStackifyLogging();
3939
40-
StackifyLib.Config.Environment = env.EnvironmentName;
40+
StackifyLib.Config.Environment = env.EnvironmentName;*/
41+
42+
string filePath = "C:\\Source\\stackify-api-dotnet\\samples\\CoreWebApp\\Stackify.json";
43+
44+
Config.ReadStackifyJSONConfig(filePath);
45+
Debug.WriteLine(StackifyLib.Config.AppName);
46+
Debug.WriteLine(StackifyLib.Config.Environment);
47+
Debug.WriteLine(StackifyLib.Config.ApiKey);
4148
}
4249

4350
private void StackifyAPILogger_OnLogMessage(string data)
@@ -82,9 +89,8 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerF
8289
// var path = Path.Combine(Directory.GetCurrentDirectory(), "nlog.config");
8390
// NLog.LogManager.Configuration = new XmlLoggingConfiguration(path, true);
8491
// app.AddNLogWeb();
85-
app.ConfigureStackifyLogging(Configuration);
86-
87-
92+
//app.ConfigureStackifyLogging(Configuration);
93+
8894
app.UseMvc();
8995
}
9096
}

0 commit comments

Comments
 (0)