fredag, august 14, 2009

Lave en log fil med tekst

Den nedenstående kode kan bruges til at skrive en log fil...

Eller skrive i event loggen. Under applikation...


Ikke specielt for sharepoint.


Husk at system brugeren skal have rettigheder til at skrive i fil mappen.
c:\\MyLogFile\\File.log

Det er jo så at sige almindelig adgang til fil systemet

using System;
using System.Collections.Generic;using System.Linq;
using System.Text;
using System.Diagnostics;
using Microsoft.SharePoint;
using System.IO;

namespace ConsoleLoggingEvent
{
class Program
{
static void Main(string[] args)
{
EventLog.WriteEntry("MyTestApp", "The application started successfully", EventLogEntryType.Information, 12345);

EventLog.WriteEntry("MyTestApp", "Application Warning", EventLogEntryType.Warning, 12345);

EventLog.WriteEntry("MyTestApp", "Application throw an exception", EventLogEntryType.Error, 12345);
}

private void WriteLog(object filetxt)
{
try
{
FileInfo fi = new FileInfo("c:\\MyLogFile\\File.log");
StreamWriter sw = fi.AppendText();

sw.WriteLine(DateTime.Now + " (" + Environment.UserName + " MyApplication)" + ": " + filetxt);
sw.WriteLine();
sw.Flush();
sw.Close();

}
catch (Exception ex)
{
Console.WriteLine(ex.Message.ToString());
}
}
}

}

Ingen kommentarer: