Question

How can i make output in txt? but not in the event log

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);
}
Was it helpful?

Solution

instead of writing to Event log you should write to text file

Add this method

 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);
      }

 }

then change this line

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

to

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

OTHER TIPS

Use File.WriteAllText method.

string path = @"c:\temp\MyTest.txt";
string createText = "Hello and Welcome" + Environment.NewLine;
File.WriteAllText(path, createText, Encoding.UTF8);
    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);
        }
    }

Something like this. Use StreamWriter object.

EDIT Obviously make sure you have access to write to the C:\ drive etc or wherever you want to write too.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top