System.Diagnostics.EventLog log = new System.Diagnostics.EventLog("TestApp");log.WriteEntry("This is a log string.");

解决方案 »

  1.   

    在调用方法或事件时,写人log即可
      

  2.   

    to:ET2004(ET2004)  我是在.net compact framework下做的,好像不支持Evengtlog 的
       还有没有什么别的方法 啊
    to:yanyzty(歪打正着) 我是想能够自动的写入啊,而不需要程序员每次都要将那些信息写入
    谢谢各位指点了啊:)
      

  3.   

    .NET Compact Framework? 你的开发平台是 Windows CE 吧?是不是 Windows CE 本身没有事件日志?
    如果是这样的话,就只能写入文件日志了。下面的代码是基于 .NET Framework 1.1 的,不知道 .NET Compact Framework 是否支持。using( StreamWriter writer = new StreamWriter(logFilename, true, System.Text.Encoding.Unicode) )
    {
    string logTime = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss\t", CultureInfo.InvariantCulture);
    string logType = String.Format(CultureInfo.InvariantCulture, "[{0}]\t", type);
    string message = String.Format(CultureInfo.InvariantCulture, logString, args);

    writer.Write(logTime);
    writer.Write(logType);
    writer.WriteLine(message);

    writer.Close();
    }
      

  4.   

    忘记加上方法原型了:(EventLogEntryType 可以用 object 替换)public static void WriteFileLog(EventLogEntryType type, string logFilename, string logString, params object[] args)
      

  5.   

    在我是在.net compact framework下做的,好像不支持Evengtlog 的
       还有没有什么别的方法 啊
      

  6.   

    .NET Compact Framework也就是WinCE或者WinCE.NET中没有log,你就用XML自己定义一个log就可以了写log的Class如下using System;namespace Cnming.Classes.Base
    {
    /// <summary>
    /// ClassEventLog 的摘要说明。
    /// </summary>
    public class ClassEventLog
    {
    private System.Diagnostics.EventLog eventLogService;

    public ClassEventLog()
    {
    this.eventLogService = new System.Diagnostics.EventLog();
    // 
    // eventLogService
    // 
    this.eventLogService.Log = "Application";
    this.eventLogService.Source = "LogTest";
    }
    #region 写日志
    /// <summary>
    /// 写日志
    /// </summary>
    public void Write_Event(string p_strSource, string p_strMessage, System.Diagnostics.EventLogEntryType p_EventType)
    {
    try
    {
    int m_intEventID = 10000;
    eventLogService.Source = p_strSource;
    eventLogService.WriteEntry(p_strMessage, p_EventType, m_intEventID);
    }
    catch
    {
    }
    }
    #endregion
    }
    }