1+ using System ;
2+ using System . Collections . Generic ;
3+ using System . Linq ;
4+ using System . Text ;
5+ using System . IO ;
6+ using System . Reflection ;
7+ using System . Windows . Forms ;
8+
9+ namespace PrintApp . Common
10+ {
11+ public class Loger
12+ {
13+
14+ public static void WriteLog ( string Message )
15+ {
16+ if ( System . Configuration . ConfigurationManager . AppSettings [ "Log" ] != null
17+ && "TRUE" . Equals ( System . Configuration . ConfigurationManager . AppSettings [ "Log" ] . ToUpper ( ) ) )
18+ {
19+ string filePath = Path . Combine ( Application . StartupPath , "Logs" ) ;
20+ if ( ! Directory . Exists ( filePath ) )
21+ {
22+ Directory . CreateDirectory ( filePath ) ;
23+ }
24+ List < string > list = new List < string > ( ) ;
25+ list . Add ( string . Format ( "时间:{0}----------------------------------------------------------------------" , DateTime . Now . ToString ( "yyyy-MM-dd HH:mm:ss" ) ) ) ;
26+ list . Add ( Message ) ;
27+
28+ try
29+ {
30+ if ( ! Directory . Exists ( filePath ) )
31+ {
32+ Directory . CreateDirectory ( filePath ) ;
33+ }
34+
35+ File . AppendAllLines ( Path . Combine ( filePath , DateTime . Now . ToString ( "yyyy-MM-dd" ) + ".log" ) , list , Encoding . Default ) ;
36+ }
37+ catch
38+ { }
39+ }
40+ }
41+
42+ public static void WriteLog ( Exception ex )
43+ {
44+ if ( System . Configuration . ConfigurationManager . AppSettings [ "Log" ] == null
45+ || ! "TRUE" . Equals ( System . Configuration . ConfigurationManager . AppSettings [ "Log" ] . ToUpper ( ) ) )
46+ {
47+ return ;
48+ }
49+
50+ StringBuilder sb = new StringBuilder ( ) ;
51+ sb . AppendLine ( string . Format ( "[ERRORMESSAGE]:{0}" , ex . Message ) ) ;
52+ sb . AppendLine ( string . Format ( "[INNEREXCEPTION]:{0}" , ex . InnerException ) ) ;
53+ sb . AppendLine ( string . Format ( "[SOURCE]:{0}" , ex . Source ) ) ;
54+ sb . AppendLine ( string . Format ( "[STACKTRACE]:{0}" , ex . StackTrace ) ) ;
55+ WriteLog ( sb . ToString ( ) ) ;
56+ }
57+ }
58+ }
0 commit comments