c#

位置:IT落伍者 >> c# >> 浏览文章

在C#中使用LOG4NET(winform程序)


发布日期:2022年04月30日
 
在C#中使用LOG4NET(winform程序)

下载lognet (Google lognet)

unzip lognet

运行VS新建 c# Windows应用程序

添加引用LogNET

新建一个应用程序配置文件nfig(具体内容附在后面)

打开Formcs

在Namespace上添加一行 [assembly: lognetConfigDOMConfigurator(Watch=true)]

(或者 编辑Assemblycs文件添加如下内容:

[assembly:lognetConfigDOMConfigurator( ConfigFileExtension=configWatch=true)] )

在类Form中添加一个静态变量

        private static readonly lognetILog log = lognetLogManagerGetLogger(SystemReflectionMethodBaseGetCurrentMethod()DeclaringType);

添加一个按钮在按钮处理函数中添加一行 logWarn(你好!);

运行程序点一下按钮

OK打开Bin\Debug\logfiletxt可以看到你好

附nfig

<?xml version= encoding=utf ?>

<configuration>

<! Register a section handler for the lognet section >

<configSections>

<section name=lognet type=SystemConfigurationIgnoreSectionHandler />

</configSections>

<appSettings>

<! To enable internal lognet logging specify the following appSettings key >

<! <add key=lognetInternalDebug value=true/> >

</appSettings>

<! This section contains the lognet configuration settings >

<lognet>

<! Define some output appenders >

<appender name=LogFileAppender type=lognetAppenderFileAppender>

  <param name=File value=logfiletxt />

  <! Example using environment variables in params >

  <! <param name=File value=${TMP}\\logfiletxt /> >

  <param name=AppendToFile value=true />

  <! An alternate output encoding can be specified >

  <! <param name=Encoding value=unicodeFFFE /> >

  <layout type=lognetLayoutPatternLayout>

  <param name=Header value=[Header]\r\n />

  <param name=Footer value=[Footer]\r\n />

  <param name=ConversionPattern value=%d [%t] %p %c [%x] <%X{auth}> %m%n />

  </layout>

  <! Alternate layout using XML  

  <layout type=lognetLayoutXMLLayout /> >

</appender>

<! Setup the root category add the appenders and set the default level >

<root>

  <level value=ALL />

  <appenderref ref=LogFileAppender />

  <! <appenderref ref=A /> >

</root>

<! Specify the level for some specific categories >

<logger name=SLognetForm>

  <! <appenderref ref=B /> >

  <level value=ALL />

  <appenderref ref=RollingLogFileAppender />

</logger>

</lognet>

</configuration>

nfig

<?xml version= encoding=utf ?>

<configuration>

<! Register a section handler for the lognet section >

<configSections>

  <section name=lognet type=SystemConfigurationIgnoreSectionHandler />

</configSections>

<appSettings>

  <! To enable internal lognet logging specify the following appSettings key >

  <! <add key=lognetInternalDebug value=true/> >

</appSettings>

<! This section contains the lognet configuration settings >

<lognet>

  <! Define some output appenders >

  <appender name=LogFileAppender type=lognetAppenderFileAppender>

   <param name=File value=logfiletxt />

   <! Example using environment variables in params >

   <! <param name=File value=${TMP}\\logfiletxt /> >

   <param name=AppendToFile value=true />

   <! An alternate output encoding can be specified >

   <! <param name=Encoding value=unicodeFFFE /> >

   <layout type=lognetLayoutPatternLayout>

    <param name=Header value=[Header]\r\n />

    <param name=Footer value=[Footer]\r\n />

    <param name=ConversionPattern value=%d [%t] %p %c [%x] <%X{auth}> %m%n />

   </layout>

   <! Alternate layout using XML  

  <layout type=lognetLayoutXMLLayout /> >

  </appender>

<! Setup the root category add the appenders and set the default level >

  <root>

   <level value=ALL />

   <appenderref ref=LogFileAppender />

   <! <appenderref ref=A /> >

  </root>

  <! Specify the level for some specific categories >

  <logger name=SLognetForm>

   <! <appenderref ref=B /> >

   <level value=ALL />

   <appenderref ref=RollingLogFileAppender />

  </logger>

</lognet>

</configuration>

//调用

using System;

using SystemCollectionsGeneric;

using SystemComponentModel;

using SystemData;

using SystemDrawing;

using SystemText;

using SystemWindowsForms;

using lognet;

[assembly: lognetConfigDOMConfigurator(Watch = true)]

namespace WindowsApplication

{

public partial class Form : Form

  {

    public Form()

    {

      InitializeComponent();

    }

private static readonly lognetILog log = lognetLogManagerGetLogger(SystemReflectionMethodBaseGetCurrentMethod()DeclaringType);

private void button_Click(object sender EventArgs e)

    {

      logWarn(你好!);

    }

  }

               

上一篇:C#中使用XML基于DOM的案例分析

下一篇:C#窗体的应用