log4net 写日志文件

代码:
using System;
using System.Configuration;
using System.Data;


using log4net;
using log4net.Config;
using log4net.Appender;
using log4net.Layout;
using log4net.Util;


namespace BizTalk.DAL.Log
{
    
/// <summary>
    
/// 日志文件处理
    
/// </summary>

    public sealed class LogUtil
    
{
        
变量

        
/// <summary>
        
/// Ctr
        
/// </summary>

        public LogUtil() { }

        
获得参数设置



        
写日志文件Method

        
/// <summary>
        
/// 取得日志文件位置和样式
        
/// </summary>
        
/// <param name="filePath">文件路径</param>
        
/// <returns></returns>

        private static FileAppender GetFileAppender(string filePath)
        
{
            
//日志文件布局
            PatternLayout sLayout = new PatternLayout("%date [%thread] %-5level %logger [%ndc] - %message%newline");
            sLayout.Header 
= "[Header]\r\n";
            sLayout.Footer 
= "[Footer]\r\n";

            
//日志文件路径
            string fileName = filePath + "\\" + System.DateTime.Now.ToString("yyyy-MM-dd")
                              
+ "\\" + System.DateTime.Now.ToString("yyyy-MM-dd"+ "-Log.log";
            FileAppender fa 
= new FileAppender(sLayout, fileName);
            
return fa;
        }

    }

}


using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
namespace BizTalk.DAL.Log
{
    
public class LogBase
    
{
        
private static void Write(string msg, string category)
        
{
            
string path = string.Format("c:\\{0}.log", category);
            
using (System.IO.StreamWriter sw = new System.IO.StreamWriter(path, true))
            
{
                
try
                
{
                    
string lin = string.Format("[{2}] {0}\r\n:{1}", category, msg, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                    sw.WriteLine(lin);
                    sw.Close();
                }

                
catch (Exception e)
                
{
                    Console.WriteLine(e);
                    sw.Close();
                }

            }

        }

        
public static void TraceWrite(string message, string category)
        
{
            
try
            
{
                
if (category == "I")
                
{
                    TraceSwitch mySwitch 
= new TraceSwitch("BizImpTrace""Biztalk trace message");
                    
if (mySwitch.TraceError)
                    
{

                        Write(message, 
"BizImpException");

                        
//Trace.Listeners[1].WriteLine(message, "BizImpException");

                    }

                }

                
else if (category == "E")
                
{
                    TraceSwitch mySwitch 
= new TraceSwitch("BizExTrace""Biztalk trace message");
                    
if (mySwitch.TraceError)
                    
{
                        
//System.Diagnostics.DefaultTraceListener = Trace.Listeners[2];
                        Write(message, "BizExException");
                        
//Trace.Listeners[2].WriteLine(message, "BizImpException");
                    }

                }

                
else if (category == "R")
                
{
                    TraceSwitch mySwitch 
= new TraceSwitch("BizRouterTrace""Biztalk trace message");
                    
if (mySwitch.TraceError)
                    
{
                        
//System.Diagnostics.DefaultTraceListener = Trace.Listeners[2];
                        Write(message, "BizRouterException");
                        
//Trace.Listeners[3].WriteLine(message, "BizRouterException");
                    }

                }

                
else
                
{
                    
//if (mySwitch.TraceError)
                    
//{
                    
//System.Diagnostics.DefaultTraceListener = Trace.Listeners[2];
                    Write(message, "BizException");
                    
//Trace.Listeners[0].WriteLine(message, "BizRouterException");
                    
//}
                }

            }

            
catch { }

        }

    }

}


配置文件增加:
  <add key="logFilePath" value="c:\log-file\"/>
      
<add key ="ErrFilePath" value ="c:\log-err\"/>

在指定的目录,按照日期生成日志文件
posted @ 2008-07-18 19:27  jhtchina  阅读(1562)  评论(0)    收藏  举报