1

我怎样才能在txt中输出?但不在事件日志中

public class ProjectHandler:Microsoft.Office.Project.Server.Events.ProjectEventReceiver {

}

public static void WriteToEventLog(string textLog, EventLogEntryType logtype)  {
    EventLog eventlog = new EventLog();
    eventlog.Source = "Project Event Handler";
    eventlog.WriteEntry(logtype.ToString() + ":" + textLog, logtype);

}

public override void OnDeleting(PSContextInfo contextInfo, ProjectPreEventArgs e) {

    WriteToEventLog(string.Format("Пользователь \"{0}\" удалил проект \"{1}\"", contextInfo.UserName, e.ProjectName), EventLogEntryType.Information);

    base.OnDeleting(contextInfo, e);
}
4

3 回答 3

1

而不是写入事件日志,您应该写入文本文件

添加这个方法

 public static void WriteToTextFile(string textLog)
 {
    FileStream objFS = null;


    string strFilePath = AppDomain.CurrentDomain.BaseDirectory + @"\Exception Log\" + System.DateTime.Now.ToString("yyyy-MM-dd ") + "Exception.log";
    if (!File.Exists(strFilePath))
    {
          objFS = new FileStream(strFilePath, FileMode.Create);
    }
    else
          objFS = new FileStream(strFilePath, FileMode.Append);

    using (StreamWriter Sr = new StreamWriter(objFS))
     {
         Sr.WriteLine(System.DateTime.Now.ToShortTimeString() + "---" + textLog);
      }

 }

然后改变这一行

 WriteToEventLog(string.Format("Пользователь \"{0}\" удалил проект \"{1}\"", contextInfo.UserName, e.ProjectName), EventLogEntryType.Information);

 WriteToTextFile(string.Format("Пользователь \"{0}\" удалил проект \"{1}\"", contextInfo.UserName, e.ProjectName));
于 2013-07-19T07:17:00.693 回答
0
    public override void OnDeleting(PSContextInfo contextInfo, ProjectPreEventArgs e)
    {
        using (System.IO.StreamWriter sw = new System.IO.StreamWriter(@"C:\Blah.txt"))
        {
            string textToWrite = string.Format("Пользователь \"{0}\" удалил проект \"{1}\"", contextInfo.UserName, e.ProjectName);
            sw.WriteLine(textToWrite);
        }
    }

像这样的东西。使用 StreamWriter 对象。

编辑 显然,请确保您有权写入 C:\ 驱动器等或您想写入的任何位置。

于 2013-07-19T07:16:06.043 回答
0

使用File.WriteAllText方法。

string path = @"c:\temp\MyTest.txt";
string createText = "Hello and Welcome" + Environment.NewLine;
File.WriteAllText(path, createText, Encoding.UTF8);
于 2013-07-19T07:18:11.373 回答