Taylohtio/GeneralApi/GeneralApi.IntegrationUtility/Services/Impl/SettingsProvider.cs

48 lines
1.2 KiB
C#

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using log4net;
using Microsoft.Practices.ServiceLocation;
namespace Taloyhtio.GeneralApi.IntegrationUtility.Services.Impl
{
public class SettingsProvider : ISettingsProvider
{
private ILog logger = ServiceLocator.Current.GetInstance<ILog>();
public T Get<T>(string key) where T : class
{
try
{
return get<T>(key);
}
catch (Exception x)
{
this.logger.ErrorFormat(string.Format("{0}\n{1}", x.Message, x.StackTrace));
return null;
}
}
public T? GetVal<T>(string key) where T : struct
{
try
{
return get<T>(key);
}
catch (Exception x)
{
this.logger.ErrorFormat(string.Format("{0}\n{1}", x.Message, x.StackTrace));
return null;
}
}
private T get<T>(string key)
{
string val = ConfigurationManager.AppSettings[key];
return (T) Convert.ChangeType(val, typeof (T));
}
}
}