Search code examples
c#outputevent-log

Output in txt in C# Class library


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

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