关于log4net的问题

hebaobao19880921 2011-08-05 02:25:20
看了网上的一些配置 自己试了下 没通过 有错误.查询未果 来CSDN问问.
问题1 我是三层模式下使用log4net的.那么我引用她的DLL是在DAL层还是UI层?
问题2.我的web.config文件中已经存在了很多配置文件的节点 我看网上配置log4net的时候完全就只有
log4net的配置节点,我也试过将其混入原来的里面还是报错.
问题3.项目中自动生成的 AssemblyInfo.cs 中[assembly: log4net.Config.DOMConfigurator()]这个跟带参数的写法的区别是什么?

希望各位使用过的人说一下.
我现在的配置文件如下:

<?xml version="1.0"?>
<configuration>
<configSections>
<sectionGroup name="system.web.extensions" type="System.Web.Configuration.SystemWebExtensionsSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<sectionGroup name="scripting" type="System.Web.Configuration.ScriptingSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<section name="scriptResourceHandler" type="System.Web.Configuration.ScriptingScriptResourceHandlerSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
<sectionGroup name="webServices" type="System.Web.Configuration.ScriptingWebServicesSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<section name="jsonSerialization" type="System.Web.Configuration.ScriptingJsonSerializationSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="Everywhere"/>
<section name="profileService" type="System.Web.Configuration.ScriptingProfileServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
<section name="authenticationService" type="System.Web.Configuration.ScriptingAuthenticationServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
<section name="roleService" type="System.Web.Configuration.ScriptingRoleServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
</sectionGroup>
</sectionGroup>
</sectionGroup>
</configSections>
<appSettings>
<add key="ConStringEncrypt" value="false"/>
<!-- 数据库连接字符串,(如果采用加密方式,上面一项要设置为true;)
如果使用明文这样server=127.0.0.1;database=.....,上面则设置为false。 -->
<add key="ConnectionString" value="server=.;database=AS_NRM;uid=sa;pwd=sa"/>
</appSettings>
<system.web>
<customErrors defaultRedirect="404.htm" mode="Off">
<error statusCode="404" redirect="404.htm"/>
</customErrors>
<sessionState mode="InProc" timeout="30"></sessionState>
<identity impersonate="true"/>
<!--
设置 compilation debug="true" 可将调试符号插入
已编译的页面中。但由于这会
影响性能,因此只在开发过程中将此值
设置为 true。
-->
<httpRuntime maxRequestLength="2097151" executionTimeout="3600"/>
<compilation debug="true">
<assemblies>
<add assembly="System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
<add assembly="System.Data.DataSetExtensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
<add assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add assembly="System.Xml.Linq, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
</assemblies>
</compilation>
<!--
通过 <authentication> 节可以配置 ASP.NET 用来
识别进入用户的
安全身份验证模式。
-->
<authentication mode="Windows"/>
<!--
如果在执行请求的过程中出现未处理的错误,
则通过 <customErrors> 节可以配置相应的处理步骤。具体说来,
开发人员通过该节可以配置
要显示的 html 错误页
以代替错误堆栈跟踪。

<customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
<error statusCode="403" redirect="NoAccess.htm" />
<error statusCode="404" redirect="FileNotFound.htm" />
</customErrors>
-->
<pages>
<controls>
<add tagPrefix="asp" namespace="System.Web.UI" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add tagPrefix="asp" namespace="System.Web.UI.WebControls" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
</controls>
</pages>
<httpHandlers>
<remove verb="*" path="*.asmx"/>
<add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" validate="false"/>
</httpHandlers>
<httpModules>
<add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
</httpModules>
</system.web>
<system.codedom>
<compilers>
<compiler language="c#;cs;csharp" extension=".cs" warningLevel="4" type="Microsoft.CSharp.CSharpCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<providerOption name="CompilerVersion" value="v3.5"/>
<providerOption name="WarnAsError" value="false"/>
</compiler>
</compilers>
</system.codedom>
<!--
在 Internet 信息服务 7.0 下运行 ASP.NET AJAX 需要 system.webServer
节。对早期版本的 IIS 来说则不需要此节。
-->
<system.webServer>
<validation validateIntegratedModeConfiguration="false"/>
<modules>
<remove name="ScriptModule"/>
<add name="ScriptModule" preCondition="managedHandler" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
</modules>
<handlers>
<remove name="WebServiceHandlerFactory-Integrated"/>
<remove name="ScriptHandlerFactory"/>
<remove name="ScriptHandlerFactoryAppServices"/>
<remove name="ScriptResource"/>
<add name="ScriptHandlerFactory" verb="*" path="*.asmx" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add name="ScriptHandlerFactoryAppServices" verb="*" path="*_AppService.axd" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add name="ScriptResource" preCondition="integratedMode" verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
</handlers>
</system.webServer>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Web.Extensions" publicKeyToken="31bf3856ad364e35"/>
<bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="3.5.0.0"/>
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Extensions.Design" publicKeyToken="31bf3856ad364e35"/>
<bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="3.5.0.0"/>
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>



不嫌麻烦可以帮我添加进去.
其他地方的配置也可以简单说说 谢谢!
...全文
123 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
laolaolai 2011-08-05
  • 打赏
  • 举报
回复
放到UI层。
不要写到web.config中,它有自己的NLog.config文件。
默认先从web.config同级目录查找NLog.config文件。

这是我以前自己学习Nlog时写的,希望对你有帮助。另外,网上帮助文档很多,你也可以去官网看帮助文档。

<?xml version="1.0" encoding="utf-8" ?>
<!--
This file needs to be put in the application directory. Make sure to set
'Copy to Output Directory' option in Visual Studio.
-->
<!--<configuration>
<configSections>
--><!--注册nlog节点--><!--
<section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog"></section>
</configSections>-->
<!--NLog配置文件NLog.config必须与Web.config文件处于同级目录-->
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" autoReload="true">
<!--定义布局变量-->
<variable name="OutFormatter"
value="引发时间:${date}
电脑名称:${machinename}
用户标识:${windows-identity}
跟踪情况:${stacktrace}
详细情况:${message}
-----------------------------------------------------
"/>


<!--定义扩展库
注意事项:(1)扩展节必须写在其它节的前面
(2)扩展库必须与NLog库处于同级目录-->
<extensions>
<add assembly="NLogExtension"/>
</extensions>

<!--******定义日志的输出目标**********-->
<targets>
<!--(1)写入文件-->
<target name="file" xsi:type="File" fileName="c:\\nlog.txt"
layout="${OutFormatter}" />

<!--(2)写入系统应用日志-->
<target name="eventlog" xsi:type="EventLog" layout="${OutFormatter}"
source="写入系统应用日志测试" log="Application"/>

<!--(3)写入数据库-->
<target name ="database" xsi:type="Database">
<dbProvider>mssql</dbProvider>
<!-- database connection parameters -->
<!-- alternatively you could provide a single 'connectionstring' parameter -->
<!--<dbhost>hostname\SQLEXPRESS</dbhost>
<dbdatabase>log</dbdatabase>
<dbusername>sa</dbusername>
<dbpassword>sa</dbpassword>-->
<connectionString>Data Source=hostname\SQLEXPRESS;Initial Catalog=Log;Integrated Security=True</connectionString>
<commandText>
insert into logInfo(time_stamp,level,logger,message,username) values(@time_stamp, @level, @logger, @message,@username);
</commandText>
<parameter name="@time_stamp" layout="${date}"/>
<parameter name="@level" layout="${level}"/>
<parameter name="@logger" layout="${logger}"/>
<parameter name="@message" layout="${message}"/>
<!--<parameter name="@username" layout="${aspnet-session:variable=UserName}"/>-->
<parameter name="@username" layout="${UserName:UserName=NLogExtensionTest}"/>

</target>

<!--(4)写入邮件-->
<target name="mail" xsi:type="Mail"
smtpServer="192.168.0.37"
smtpUsername="zhangsan" smtpPassword="mailpwd" smtpAuthentication="Basic"
from="zhangsan@sina.com" to="zhangsan@sina.com"
subject="写入邮件日志测试"
layout="${OutFormatter}"
/>

<!--<target name="network" xsi:type="Network" address="tcp://localhost:5555"
layout="${level} ${logger} ${message}${newline}"/>-->

</targets>

<!--******定义日志的路由规则**********-->
<rules>
<!--<logger name="*" writeTo="file,eventlog,database,mail" />-->
<logger name="ZXWBLogger" writeTo="file" />
</rules>
</nlog>

<!--</configuration>-->






62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧