本人新手,最近跟了一个项目是基于.net winforms开发的,其中有一块儿功能是系统日志的记录:包括操作日志和异常日志。本人从网上查到几种方法,但是因为没有什么思路所以都不了解。这里小弟请前辈们指点迷津了,先谢过了:
1.分为操作日志和异常日志
2.都记录到系统日志表中(通过数据层)
3.异常日志记录到本地异常记录Log文件中
4.提供继承调用。
以上是该功能基本需求。
我的解决方法:
1)我们系统中已经有操作日志的表了,然后写个SystemLog的类,把操作日志的方法写进去
再然后,你想在那里加入对日志的操作,调用SystemLog那个类就好了。
2)建一张有关日志信息的表,带上一个日志类别的字段,写一个读写这个日志表的类,在需要记录系统日志的地方加上日志插入操作。做一个页面能根据条件查询到这些日子,不就可以了?
求解决方法,及详细代码,因为新手,所以很多不太明白,谢谢了。

解决方案 »

  1.   

    不理解呀,有代码最好了。给讲解一下
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Diagnostics;
    namespace Log
    {
        class LogWirter
        {
            /// <summary>
            /// 事件源名称
            /// </summary>
            private string eventSourceName;
            EventLogEntryType eventLogType;
            public LogWirter()
            {
                eventSourceName = "test";
                eventLogType = EventLogEntryType.Error;
            }        /// <summary>
            /// 消息事件源名称
            /// </summary>
            public string EventSourceName
            {
                set { eventSourceName = value; }
            }        /// <summary>
            /// 消息事件类型
            /// </summary>
            public EventLogEntryType EventLogType
            {
                set { eventLogType = value; }
            }        /// <summary>
            /// 写入系统日志
            /// </summary>
            /// <param name="message">事件内容</param>
            public void LogEvent(string message)
            {
                if (!EventLog.SourceExists(eventSourceName))
                {
                    EventLog.CreateEventSource(eventSourceName, "Application");
                }
                EventLog.WriteEntry(eventSourceName, message, EventLogEntryType.Error);
            }
        }
    }
    code:
     /// <summary>
     /// 操作文件实现日志
     /// </summary>
     /// <param name="msg">日志内容</param>
     /// <param name="lonPath">存放日志的目录,如果想默认存在项目的Debag下,请输入Null</param>
     private void logger(String msg,string lonPath)
     {
     string LogPath = null;
     if (lonPath != null)
     {
     LogPath = lonPath;
     }
     else
     {
     LogPath =Application.StartupPath;
     }
     string filePath = LogPath + "\\" + DateTime.Now.Year.ToString()+"年" + DateTime.Now.Month.ToString()+"月";
     string fileName = DateTime.Now.Day.ToString() + "日" + ".Log";
     string filePathAll = filePath + "\\" + fileName;
     if (!System.IO.Directory.Exists(filePath))
     {
     System.IO.Directory.CreateDirectory(filePath);
     }
     FileStream filestream = new FileStream(filePathAll, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None);
     StreamWriter writer = new StreamWriter(filestream, System.Text.Encoding.Default);
     writer.BaseStream.Seek(0, SeekOrigin.End);
     writer.WriteLine("{0} {1}", DateTime.Now.TimeOfDay , msg);
     writer.Flush();
     writer.Close();
     filestream.Close();
     }
     //测试方法
     private void button1_Click(object sender, EventArgs e)
     {
     byte str=0 ;
     {
     if (true)
     {
     str = (byte)Convert.ToSByte(600);
     }
     else
     {
     str = (byte)Convert.ToSByte(500);
     }
     }
     catch(Exception ex)
     {
     logger(ex.Message,null);//异常.message就是该异常的描述
     }
     }
    这两段代码是否可用,或者需要改进的,就按以上的需求