修改或新增AppSetting节点
/// <summary> /// 修改AppSettings中配置 /// </summary> /// <param name="key">key值</param> /// <param name="value">相应值</param> public static bool SetConfigValue(string key, string value) { try { Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); if (config.AppSettings.Settings[key] != null) config.AppSettings.Settings[key].Value = value; else config.AppSettings.Settings.Add(key, value); config.Save(ConfigurationSaveMode.Modified); ConfigurationManager.RefreshSection("appSettings"); return true; } catch { return false; } }
/// <summary>
/// 获取AppSettings中某一节点值
/// </summary>
/// <param name="key"></param>
public static string GetConfigValue(string key)
{
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
if (config.AppSettings.Settings[key] != null)
return config.AppSettings.Settings[key].Value;
else
return string.Empty;
}
获取AppSetting节点值
对log4Net 设置
<log4net>
<!--定义输出到文件中-->
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<!--定义文件存放位置-->
<file value="log\\" />
<appendToFile value="true" />
<rollingStyle value="Date" />
<datePattern value="yyyyMMdd'.txt'" />
<staticLogFileName value="false" />
<param name="MaxSizeRollBackups" value="100" />
<layout type="log4net.Layout.PatternLayout">
<!--每条日志末尾的文字说明-->
<!--输出格式-->
<!--样例:2008-03-26 13:42:32,111 [10] INFO Log4NetDemo.MainClass [(null)] - info-->
<!--<conversionPattern value="%newline %n记录时间:%date %n线程ID:[%thread] %n日志级别: %-5level %n出错类:%logger property: [%property{NDC}] - %n错误描述:%message%newline %n"/>-->
<conversionPattern value="%n记录时间:%date %n错误描述:%message %n" />
</layout>
</appender>
<root>
<level value="ALL" />
<!--文件形式记录日志-->
<appender-ref ref="RollingLogFileAppender" />
</root>
</log4net>
public static void WriteLog(Type t, Exception ex)
{
ILog log = LogManager.GetLogger(t);
log.Error("Error", ex);
}
public static void WriteLog(Type t, string msg)
{
ILog log = LogManager.GetLogger(t);
log.Error(msg);
}
在写操作日志时,要注意,需要在命名空间上加一句[assembly: XmlConfigurator(Watch = true)]