zoukankan      html  css  js  c++  java
  • 【Vegas原创】Log4Net实践

    Windows服务程序,将所出现的问题写入log

    Service1.cs

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Diagnostics;
    using System.ServiceProcess;
    using System.Text;
    using System.Net.Mail;
    [assembly: log4net.Config.XmlConfigurator(Watch = true)]

    namespace EIPMailService
    {
      
        
    public partial class Service1 : ServiceBase
        {
            DBAccess db 
    = new DBAccess();
            
    private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
            
    string strEX = "EX";

            
    public Service1()
            {
                InitializeComponent();
            }

            
    protected override void OnStart(string[] args)
            {
                
    // TODO: 在此处添加代码以启动服务。
            }

            
    protected override void OnStop()
            {
                
    // TODO: 在此处添加代码以执行停止服务所需的关闭操作。
            }

      
            
    void sendMail(string strID)
            {
                
    try
                {
                    
    //
                }
                
    catch(Exception ex)
                {
                //IF repeat,do not write
                    if (ex.ToString() == strEX)
                    {

                    }
                    
    else
                    {
                        log.Error(
    "Error", ex);
                        strEX 
    = ex.ToString();
                    }
                }

                

            }
     
            
    private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
            {
               
    //
                        sendMail(strID);
                
    //
            }
             

        }
    }

    web.config

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      
    <configSections>
        
    <!--<section name="log4net" type="System.Configuration.IgnoreSectionHandler" />-->
        
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
      
    </configSections>
      <appSettings>
        

      
    </appSettings>
      
      
    <!-- This section contains the log4net configuration settings -->
      
    <log4net>
        
    <appender name="LogFileAppender" type="log4net.Appender.FileAppender">
          
    <file value="F:\vegas.txt" />
          
    <!-- Example using environment variables in params -->
          
    <!-- <file value="${TMP}\log-file.txt" /> -->
          
    <!--<sppendToFile value="true" />-->
          
    <!-- An alternate output encoding can be specified -->
          
    <!-- <encoding value="unicodeFFFE" /> -->
          
    <layout type="log4net.Layout.PatternLayout">
            
    <footer value="[Footer]--Test By Vegas " />
            
    <conversionPattern value="%date [%thread] %-5level %logger [%ndc] &lt;%property{auth}&gt; - %message%newline" />
          
    </layout>
          
    <!-- Alternate layout using XML            
                <layout type="log4net.Layout.XMLLayout" /> 
    -->
        
    </appender>

        
    <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
          
    <!--<target value="Console.Error" />-->
          
    <layout type="log4net.Layout.PatternLayout">
            
    <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
          
    </layout>
        
    </appender>

        
    <appender name="EventLogAppender" type="log4net.Appender.EventLogAppender">
          
    <layout type="log4net.Layout.PatternLayout">
            
    <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
          
    </layout>
        
    </appender>

        
    <!-- Setup the root category, add the appenders and set the default level -->
        
    <root>
          
    <appender-ref ref="LogFileAppender" />
          
    <appender-ref ref="ConsoleAppender" />
          
    <appender-ref ref="EventLogAppender" />
        
    </root>
      
    </log4net>

    </configuration>



     

  • 相关阅读:
    飞思卡尔IMX6处理器的GPIO配置方式
    批处理清除VisualStudio解决方案文件夹
    总结过去10年的程序员生涯,给程序员小弟弟小妹妹们的一些总结性忠告
    详解Linux2.6内核中基于platform机制的驱动模型 (经典)
    [驱动注册]platform_driver_register()与platform_device_register()
    机器人系统常用仿真软件介绍效果与评价指标
    WINCE的批处理
    项目开发中的人月及如何计算
    常用的六个富文本编辑器
    如何获取公众号里面的歌曲
  • 原文地址:https://www.cnblogs.com/amadeuslee/p/3744501.html
Copyright © 2011-2022 走看看