public object Invoke(IMethodInvocation invocation) { LogDto log=new LogDto(); log.ObjName = invocation.Target.ToString(); log.MethodName = invocation.Method.Name; log.CalledTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); log.CalledUser = Context.Current["userName"].ToString(); LogHelper.logToFile(log); _log.Info(string.Format("AOP:对象{0},方法名{1}",log.ObjName,log.MethodName)); return invocation.Proceed(); }
public static void logToFile(LogDto log) { string logFilePath = AppDomain.CurrentDomain.BaseDirectory+"//log.txt"; if (!File.Exists(logFilePath)) { File.Create(logFilePath); } StreamWriter sw = new StreamWriter(logFilePath, true, Encoding.Default); //FileStream fs = new FileStream(logFilePath, FileMode.OpenOrCreate, FileAccess.Write); //StreamWriter sw = new StreamWriter(fs); sw.WriteLine("时间:{0} 用户:{1} 调用对象:{2} 的方法:{3}", log.CalledTime, log.CalledUser, log.ObjName, log.MethodName); //fs.Close(); sw.Close(); }