1、首先引用Log4Net 的命名空间
using log4net;
2、在使用的类中生命静态变量 log
public class FileService
{
static readonly ILog log = LogManager.GetLogger(typeof(FileService));
.....
.....
3、在 try catch 语句中记录异常信息
try
{
operationHandler(fileName);
return FileOperationResult.Successful;
}
catch (IOException ex)
{
log.Info("Unable to save file: " + fileName, ex);
userMessage = "A problem occured saving the file."; /* TODO: Make localizable resource. */
ioExceptionOccured = true;
}
catch (Exception ex) /* TODO: catch common IO errors and report to user. */
{
log.Info("Unable to save file: " + fileName, ex);
var userMessageException = ex as IUserMessageProvider;
if (userMessageException != null && userMessageException.UserMessagePresent)
{
userMessage = userMessageException.UserMessage;
}
}