[英]Configure log4net for asp.net MVC3 project
Ok, so I'm understood how to configure the log4Net
in my application, But now 好的,所以我理解如何在我的应用程序中配置log4Net
,但现在
First I want to improve the configuration by differencing the level of the logs if the application it's a release or a debug, how can I do this?. 首先 ,如果应用程序是发布或调试,我想通过区分日志级别来改进配置,我该怎么做?
Second , If I had a folder in my project called LOG
how can I set the configuration, to not used the physical folder of my application?? 第二 ,如果我的项目中有一个名为LOG
的文件夹,我该如何设置配置,不使用我的应用程序的物理文件夹?
for example Instead of: 例如,而不是:
<file value="C:\physicalpath\LOG\Log.log" />
used 用过的
<file value="\LOG\Log.log" />
or 要么
<file value="%some_variable%\LOG\Log.log" />
The documenation is straight forward: 该文件是直截了当的:
file : the full or relative path to the log file. file :日志文件的完整路径或相对路径。
So all you need to have is the full path like C:\\physicalpath\\LOG\\Log.log
or the ralative one, this needs to start with the dot char .
所以你需要拥有的是完整的路径,比如C:\\physicalpath\\LOG\\Log.log
或ralative one,这需要以dot char开头.
like .\\App_Data\\Log4Net.Logs
喜欢.\\App_Data\\Log4Net.Logs
you can also use the folder name in the file
attribute, then you must use the datePattern
attribute to specify the file name, for example: 您还可以在file
属性中使用文件夹名称 ,然后必须使用datePattern
属性指定文件名,例如:
<appender name="FileAppender" type="log4net.Appender.RollingFileAppender">
<file value=".\\App_Data\\Log4Net.Logs\\backend"/>
<datePattern value=".yyyy-MM-dd'.log'"/>
<appendToFile value="true"/>
<maximumFileSize value="256KB"/>
<maxSizeRollBackups value="2"/>
<rollingStyle value="Date"/>
<staticLogFileName value="false"/>
<lockingModel type="log4net.Appender.FileAppender+MinimalLock"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%level %thread %logger - %message%newline"/>
</layout>
</appender>
Also remember to add the 还记得添加
<lockingModel type="log4net.Appender.FileAppender+MinimalLock"/>
So you can avoid that log4net
lock the file and you can't used it to append your messages. 所以你可以避免log4net
锁定文件,你不能用它来附加你的消息。
If you're not used to log4net
, don't forget to add the <root>
node, this is the the one that let's log4net
know what you want to use and not the <appender>
nodes, for example, you can have 10 <appender>
nodes and use only one, the <root>
node is then only configured with the one you want to use... 如果您不习惯log4net
,请不要忘记添加<root>
节点, log4net
知道您想要使用的节点,而不是<appender>
节点,例如,您可以拥有10个节点<appender>
节点并且只使用一个, <root>
节点只配置了你想要使用的节点...
here is a full configuration with 2 Mongo Appenders and 1 File Appender, the <root>
specifies that only the file appender is in use: 这是一个包含2个Mongo Appender和1个File Appender的完整配置 , <root>
指定只使用文件追加器:
<log4net>
<appender name="MongoAppender" type="log4net.Appender.MongoDBAppender, log4mongo-net">
<!-- MongoDB 1 connection options -->
<host value="staff.mongohq.com"/>
<port value="10077"/>
<databaseName value="myApp_2011"/>
<collectionName value="logs_net"/>
<userName value="myself"/>
<password value="123456"/>
</appender>
<appender name="MongoAppenderAppHarbor" type="log4net.Appender.MongoDBAppender, log4mongo-net">
<!-- MongoDB 2 connection options -->
<host value="staff.mongohq.com"/>
<port value="10048"/>
<databaseName value="d1741d63-46b1-4a44-9c49-8c28cecae36b"/>
<collectionName value="logs_net"/>
<userName value="myself"/>
<password value="123456"/>
</appender>
<appender name="FileAppender" type="log4net.Appender.RollingFileAppender">
<!-- Local file options -->
<file value=".\\App_Data\\Log4Net.Logs\\backend"/>
<datePattern value=".yyyy-MM-dd'.log'"/>
<appendToFile value="true"/>
<maximumFileSize value="256KB"/>
<maxSizeRollBackups value="2"/>
<rollingStyle value="Date"/>
<staticLogFileName value="false"/>
<lockingModel type="log4net.Appender.FileAppender+MinimalLock"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%level %thread %logger - %message%newline"/>
</layout>
</appender>
<root>
<!--
<level value="DEBUG" />
<appender-ref ref="MongoAppender" />
<appender-ref ref="MongoAppenderAppHarbor" />
-->
<appender-ref ref="FileAppender"/>
</root>
</log4net>
Remember maybe you should to add in Global.asax.cs the follow code: 记住也许你应该在Global.asax.cs中添加以下代码:
protected void Application_Start()
{
log4net.Config.XmlConfigurator.Configure();
//...more code
}
Below is a sample app.config
section, with the Path being defined through the <file value="somepath"/>
tag. 下面是一个示例app.config
部分,其中Path是通过<file value="somepath"/>
标记定义的。
This link is a good resource for documentation 此链接是文档的良好资源
<log4net>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="C:\\SOMEPATH\\SOMELOG.log"/>
<appendToFile value="true"/>
<rollingStyle value="Size"/>
<maxSizeRollBackups value="10"/>
<maximumFileSize value="10MB"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger – %message%exception%newline"/>
</layout>
</appender>
<root>
<level value="ALL"/>
<appender-ref ref="RollingFileAppender"/>
</root>
</log4net>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.