Commit fc2426c5 authored by Charles Still's avatar Charles Still
Browse files

Add init logger

parent 0e147fc0
......@@ -60,6 +60,7 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="LoggerManager.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
......@@ -67,6 +68,7 @@
<None Include="App.config" />
<None Include="packages.config" />
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
......
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BeeWeatherPollenTracker
{
public class LoggerManager
{
private static LoggerManager Logger = null;
private String Path = "";
private FileStream fs = null;
public LoggerManager()
{
Path = ".\\logfile.txt";
if (!File.Exists(Path)) // Check if the file is existing and create him if not.
{
fs = File.Create(Path);
}
else
{
fs = File.OpenWrite(Path);
}
}
public static LoggerManager GetInstance()
{
if (Logger == null)
{
Logger = new LoggerManager();
}
return Logger;
}
public void WriteInFile(Exception e)
{
DateTime dt = DateTime.Now;
string res = dt.ToString() + " : "+ e.Message ;
byte[] data = Encoding.UTF8.GetBytes(res);
fs.Write(data, 0, data.Length);
}
}
}
......@@ -12,23 +12,34 @@ namespace BeeWeatherPollenTracker
private const string OPEN_WEATHER_MAP_API_KEY = "3f80e65ed0609e7e52737e65d522aed8";
private const double FORECAST_LENGTH = 3.0;
static LoggerManager logs = null;
private static void Main(string[] args)
{
// Set up API KEY
ClientSettings.SetApiKey(OPEN_WEATHER_MAP_API_KEY);
// Set up the logger manager
logs = LoggerManager.GetInstance();
// Retrieve forecast data from from OWM API
var cityId = int.Parse(ConfigurationManager.AppSettings["CityId"]);
var apiResults = FiveDaysForecast.GetByCityId(cityId);
if (!apiResults.Success)
try
{
throw new ApplicationException(apiResults.Message);
}
var cityId = int.Parse(ConfigurationManager.AppSettings["CityId"]);
var apiResults = FiveDaysForecast.GetByCityId(cityId);
if (!apiResults.Success)
{
throw new ApplicationException(apiResults.Message);
}
// Filter: keep only 3 next days forecast
var forecast = apiResults.Items
.Where(x => x.Date < DateTime.Now.AddDays(FORECAST_LENGTH))
.ToList();
// Filter: keep only 3 next days forecast
var forecast = apiResults.Items
.Where(x => x.Date < DateTime.Now.AddDays(FORECAST_LENGTH))
.ToList();
}
catch (Exception e)
{
logs.WriteInFile(e);
}
}
}
}
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment