日志纪录到文本和xml实现方式

interface  ILog
{
    
bool Write(string message);
    
bool Write(Exception ex);
}

 

class  TextFileLog:ILog
{
    
public bool Write(string message)
    
{
        
string fileDir = ConfigurationManager.AppSettings["LogTarget"].ToString();
        
using (StreamWriter w = File.AppendText(fileDir))
        
{
           
// w.Write(" Log Entry : ");
            w.WriteLine("发生时间{0}", DateTime.Now.ToLocalTime().ToString());
            w.WriteLine(
"日志内容为:{0}", message);
            w.WriteLine(
"-------------------------------");
            
// Update the underlying file.
            w.Flush();
            w.Close();
        }

        
return true;
    }

    
public bool Write(Exception ex)
    
{

        Write(ex.Message);
        
return true;
    }

}

 

class  XmlFileLog:ILog
    
{
        
public bool Write(string message)
        
{
            
string xmlFilePath = ConfigurationManager.AppSettings["LogTarget"].ToString();
            
if (File.Exists(xmlFilePath))
            
{
                XmlDocument doc 
= new XmlDocument();
                doc.Load(xmlFilePath);
                XmlDocumentFragment docFrag 
= doc.CreateDocumentFragment();
                XmlNode nod 
= doc.SelectSingleNode("Logs");
                docFrag.InnerXml 
= "<Log><Time>"+DateTime.Now.ToLocalTime().ToString()
                    
+"</Time><Message>"+message+"</Message></Log>";
                nod.AppendChild(docFrag);

                doc.Save(xmlFilePath);
                
return true;
            }

            
else
            
{
                XmlWriterSettings settings 
= new XmlWriterSettings();
                settings.Indent 
= true;     //设置缩进       
                settings.ConformanceLevel = ConformanceLevel.Auto;
                settings.IndentChars 
= " ";
                settings.OmitXmlDeclaration 
= false;
                
using (XmlWriter writer = XmlWriter.Create(xmlFilePath, settings))
                
{
                    
//Start writing the XML document
                    writer.WriteStartDocument(false);
                    
//Start with the root element
                    writer.WriteStartElement("Logs");
                    writer.WriteStartElement(
"Log");
                    writer.WriteStartElement(
"Time");
                    writer.WriteString(DateTime.Now.ToLocalTime().ToString());
                    writer.WriteEndElement();
                    writer.WriteStartElement(
"Message");
                    writer.WriteString(message);
                    writer.WriteEndElement();
                    writer.WriteEndElement();
                    writer.WriteEndDocument();
                    
//Flush the object and write the XML data to the file
                    writer.Flush();
                    
return true;
                }
   

            }

        }

        
public bool Write(Exception ex)
        
{
            Write(ex.Message);
            
return true;

        }

    }

测试

   class  Program
    
{
        
static void Main(string[] args)
        
{
            System.Type type 
= System.Type.GetType(ConfigurationManager.AppSettings["LogType"].ToString());
            ILog log 
= (ILog)Activator.CreateInstance(type);
            log.Write(
new Exception("异常测试"));
        }

    }

config配置为:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
 <appSettings>
  <add key="LogType" value="XmlFileLog"/>
  <!-- XmlFileLog  TextFileLog-->
  <add key="LogTarget" value="c:/log.xml"/>
 </appSettings>
</configuration>

数据库实现的话继承Ilog接口即可

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值