客户端类中中记录异常的方法: 使用Log4net

1、首先引用Log4Net 的命名空间

    using log4net;

 

2、在使用的类中生命静态变量 log

 

      public class FileService
    {
        static readonly ILog log = LogManager.GetLogger(typeof(FileService));

         .....

         .....

   3、在 try catch 语句中记录异常信息

    

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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;
                }
            }

 

     

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。