app.config中的值获取及设置 以及对log4net配置
修改或新增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)]
app.config中的值获取及设置 以及对log4net配置的更多相关文章
- .net里面<app.config>中value值不能填写特殊符号问题
配置app.config或web.config的时候,经常要填写value值, 但是value值不能包含特殊字符,需要转义, 分享一下转义字符 App.config 实际上是 xml 文件,在标准 x ...
- VS动态修改App.config中遇到的坑(宿主进程问题)
昨天遇到了很奇怪的一个bug,具体描述如下: 这个系统是c/s架构的针对多个工厂做的资材管理系统,由于有很多个工厂,每个工厂都有自己的服务器.所以需要动态的改变连接字符串去链接不同的服务器. 由于这个 ...
- 在Web.config或App.config中的添加自定义配置
.Net中的System.Configuration命名空间为我们在web.config或者app.config中自定义配置提供了完美的支持.最近看到一些项目中还在自定义xml文件做程序的配置,所以忍 ...
- winform程序读取和改写配置文件App.config元素的值
winform程序读取和改写配置文件App.config元素的值 2016-05-16 17:49 by newbirth, 2412 阅读, 0 评论, 收藏, 编辑 1 2 3 4 5 6 7 & ...
- 在Web.config或App.config中的添加自定义配置 <转>
.Net中的System.Configuration命名空间为我们在web.config或者app.config中自定义配置提供了完美的支持.最近看到一些项目中还在自定义xml文件做程序的配置 ...
- log4net日志在app.config中assembly不起作用
log4net 1.2.15.0日志在app.config中assembly不起作用,必须 1.手动调用方法log4net.Config.XmlConfigurator.Configure()来初始化 ...
- C#读写app.config中的数据
C#读写app.config中的数据 读语句: String str = ConfigurationManager.AppSettings["DemoKey"]; 写语句: Con ...
- winform中读取App.config中数据连接字符串
1.首先要在工程引用中导入System.Configuration.dll文件的引用. 2.通过System.Configuration.ConfigurationManager.Connection ...
- C#中怎样获取默认配置文件App.config中配置的键值对内容
场景 在新建一个程序后,项目中会有一个默认配置文件App.config 一般会将一些配置文件信息,比如连接数据库的字符串等信息存在此配置文件中. 怎样在代码中获取自己配置的键值对信息. 注: 博客主页 ...
随机推荐
- ES5比较Jquery中的each与map 方法?
1.each es5: var arr = [1, 5, 7, 8, 9];var arr1 = []; arr.forEach(function (v, i) { arr1.push(v * 4) ...
- GitHub上常用命令(工作中几乎每天用到的命令)
查看自己当前分支 git branch 查看远程和本地分支 git branch -a 查看origin下的分支 git config -vv git config --lis 创建分支 git br ...
- USB串行端口
USB-SERIAL CH341A(COM22)USB串行端口
- windows CE项目开发
软件列表 1.Windows mobile 设备中心 2.Microsoft visual Studio 2008 3.串口调试工具(sscom42.exe) 4.Wince 6.0模拟器 5.vir ...
- Java Web学习总结(11)——Session使用示例教程
一.Session简单介绍 在WEB开发中,服务器可以为每个用户浏览器创建一个会话对象(session对象),注意:一个浏览器独占一个session对象(默认情况下).因此,在需要保存用户数据时,服务 ...
- How to Rotate Tomcat catalina.out
If catalina.out becomes 2GB in size, tomcat crashes and fails to start without any error message. To ...
- 单位转换 inch mm mil
从上面看:英寸(inch)是最大的单位 其次是毫米(mm) 再次是密耳(mil)
- IOS蓝牙项目总结
常见的蓝牙标准有2.0和4.0. 特点 2.0 1.适用于数据量比较大得传输,比如音乐.语音2.IOS开发中,要求设备是经过MFI认证 4.0 1.适用于实时性比较高的数据传输,比如遥控类的鼠标. ...
- 关于 /etc/zabbix/zabbix_agentd.conf 文件 Hostname 文件的说明
前提 (1) /etc/hosts 文件如下 [root@testdb ~]# cat /etc/hosts 127.0.0.1 localhost localhost.localdomain loc ...
- python课程:python3的输入输出
输出函数用法 (话说python3的输出好像没有python2的灵活了) print('hello,world') #单引号和双引号都可以输出print("hello,world&quo ...