NLogを使用することもできますが、実装は非常に簡単です。
まず、NLogをプロジェクトにダウンロードして追加します。
と作成NLog.config:
<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
<targets>
<target name="file" xsi:type="File" fileName="${basedir}/Logs/site.log"
layout="${date}: ${message}" />
<target name="eventlog" xsi:type="EventLog" source="Dealing Errors Nlog" log="Application"
layout="${date}: ${message} ${stacktrace}" />
</targets>
<rules>
<logger name="*" minlevel="Info" writeTo="file" />
<logger name="*" minlevel="Error" writeTo="file" />
<logger name="*" minlevel="Error" writeTo="eventlog" />
<logger name="*" minlevel="Info" writeTo="eventlog" />
</rules>
</nlog>
Global.asaxの
private NLogLogger GetLog()
{ return new NLogLogger(); }
クリートログという名前の1つのクラスにおいて。CS:
public static class Log
{
public static ILogger GetLogger()//return the interface
{
return new NLogLogger();
}
}
NLoggerという名前の別のクラスを作成します。インターフェイスILoggerです上の
public class NLogLogger : ILogger
{
private Logger _logger; //NLog
public NLogLogger()
{
_logger = LogManager.GetCurrentClassLogger();
}
public void Info(string message)
{
_logger.Info(message);
}
public void Warn(string message)
{
_logger.Warn(message);
}
public void Debug(string message)
{
_logger.Debug(message);
}
public void Error(string message)
{
_logger.Error(message);
}
public void Error(Exception x)
{
Error(LogUtility.DealingException(x));
}
public void Fatal(string message)
{
_logger.Fatal(message);
}
public void Fatal(Exception x)
{
Fatal(LogUtility.DealingException(x));
}
}
とCREAD:
public interface ILogger
{
void Info(string message);
void Warn(string message);
void Debug(string message);
void Error(string message);
void Error(Exception x);
void Fatal(string message);
void Fatal(Exception x);
}
、最終的に1つのクラスLogUtility:
public class LogUtility
{
public static string DealingException(Exception x)
{
Exception logException = x;
if (x != null)
{
logException = x;
}
string strErrorMsg = Environment.NewLine + "URL ERROR:\t" + System.Web.HttpContext.Current.Request.Path;
strErrorMsg += Environment.NewLine + "URL:\t" + System.Web.HttpContext.Current.Request.RawUrl;
strErrorMsg += Environment.NewLine + "Message:\t" + (logException.Message != null ? logException.Message : "Not Found");
strErrorMsg += Environment.NewLine + "Source:\t" + (logException.Source != null ? logException.Source : "Not Found");
strErrorMsg += Environment.NewLine + "Stack Trace:\t" + (logException.StackTrace != null ? logException.StackTrace : "Not found");
strErrorMsg += Environment.NewLine + "Destination URL:\t" + (logException.TargetSite.ToString() != null ? logException.TargetSite.ToString() : "Not found");
strErrorMsg += Environment.NewLine;
strErrorMsg += Environment.NewLine;
return strErrorMsg;
}
}
がそのことを確認しますアプリケーションプールユーザーに書き込み許可が与えられているイオンをあなたが書き込んでいるディレクトリに移動します。 – Brian