VS - Microsoft.Practices.EnterpriseLibrary.Logging
string fileName = AppDomain.CurrentDomain.BaseDirectory + "\\log.txt";
File.AppendAllText(fileName, "start\r\n", Encoding.UTF8);
version 6.0.0.0 need set ExceptionManager
PM> Install-Package EnterpriseLibrary.Logging
PM> Install-Package EnterpriseLibrary.Common
PM> Install-Package EnterpriseLibrary.ExceptionHandling
PM> Install-Package EnterpriseLibrary.ExceptionHandling.Logging
Microsoft.Practices.EnterpriseLibrary.ConfigConsoleV6.vsix。这个工具的下载地址:http://www.microsoft.com/en-us/download/details.aspx?id=38789
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | <section name= "loggingConfiguration" type= "Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission= "true" /> <section name= "exceptionHandling" type= "Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration.ExceptionHandlingSettings, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission= "true" /> using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling; using Microsoft.Practices.EnterpriseLibrary.Logging; using Microsoft.Practices.EnterpriseLibrary.Common.Configuration; static AppLogger() { Logger.SetLogWriter( new LogWriterFactory().Create()); IConfigurationSource config = ConfigurationSourceFactory.Create(); ExceptionPolicyFactory factory = new ExceptionPolicyFactory(config); ExceptionManager exManager = factory.CreateManager(); ExceptionPolicy.SetExceptionManager(factory.CreateManager()); } public static void LogError(Exception ex) { lock (staticLock) { ExceptionPolicy.HandleException(ex, AppConfiguration.ExceptionPolicyName); } } |
App.config
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 | <?xml version= "1.0" encoding= "utf-8" ?> <configuration> <configSections> <section name= "loggingConfiguration" type= "Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission= "true" /> <section name= "exceptionHandling" type= "Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration.ExceptionHandlingSettings, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission= "true" /> <section name= "unity" type= "Microsoft.Practices.Unity.Configuration.UnityConfigurationSection,Microsoft.Practices.Unity.Configuration" /> <sectionGroup name= "applicationSettings" type= "System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" > <section name= "Common.Properties.Settings" type= "System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission= "false" /> </sectionGroup> </configSections> <unity xmlns= "http://schemas.microsoft.com/practices/2010/unity" > <container> <register type= "Common.Interfaces.IGetServiceResponseBLL,R365.Common" mapTo= "Common.BusinessLogic.CallTestBLL,Common" /> </container> </unity> <applicationSettings> <Common.Properties.Settings> </Common.Properties.Settings> </applicationSettings> <startup> <supportedRuntime version= "v4.0" sku= ".NETFramework,Version=v4.5" /> </startup> <loggingConfiguration name= "" tracingEnabled= "true" defaultCategory= "General" > <listeners> <add name= "Event Log Listener" type= "Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FormattedEventLogTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" listenerDataType= "Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FormattedEventLogTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" source= "test Logging" formatter= "Text Formatter" log= "" machineName= "." traceOutputOptions= "None" /> <add name= "Email Trace Listener" type= "Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.EmailTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" listenerDataType= "Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.EmailTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" toAddress= "test@test.com.cn" fromAddress= "test@test.com.mo" subjectLineStarter= "[test] " subjectLineEnder= "- DEV" smtpServer= "smtpserver.test.com.cn" formatter= "Text Formatter" /><br> <add name= "Database Trace Listener" type= "Microsoft.Practices.EnterpriseLibrary.Logging.Database.FormattedDatabaseTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging.Database, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" <br> listenerDataType= "Microsoft.Practices.EnterpriseLibrary.Logging.Database.Configuration.FormattedDatabaseTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging.Database, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" <br> databaseInstanceName= "TestConnectionString" writeLogStoredProcName= "WriteLog" <br> addCategoryStoredProcName= "AddCategory" formatter= "Text Formatter" traceOutputOptions= "None" /> </listeners> <formatters> <add type= "Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" template="Timestamp: {timestamp}{newline} Message: {message}{newline} Category: {category}{newline} Priority: {priority}{newline} EventId: {eventid}{newline} Severity: {severity}{newline} Title:{title}{newline} Machine: {localMachine}{newline} App Domain: {localAppDomain}{newline} ProcessId: {localProcessId}{newline} Process Name: {localProcessName}{newline} Thread Name: {threadName}{newline} Win32 ThreadId:{win32ThreadId}{newline} Extended Properties: {dictionary({key} - {value}{newline})}" name= "Text Formatter" /> </formatters> <categorySources> <add switchValue= "All" name= "General" > <listeners> <add name= "Event Log Listener" /> </listeners> </add> <add switchValue= "All" name= "Error" > <listeners> <add name= "Event Log Listener" /> <add name= "Email Trace Listener" /><br> <add name= "Database Trace Listener" /> </listeners> </add> </categorySources> <specialSources> <allEvents switchValue= "All" name= "All Events" /> <notProcessed switchValue= "All" name= "Unprocessed Category" /> <errors switchValue= "All" name= "Logging Errors & Warnings" > <listeners> <add name= "Event Log Listener" /> </listeners> </errors> </specialSources> </loggingConfiguration> <exceptionHandling> <exceptionPolicies> <add name= "Exception Policy" > <exceptionTypes> <add name= "All Exceptions" type= "System.Exception, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" postHandlingAction= "NotifyRethrow" > <exceptionHandlers> <add name= "Logging Exception Handler" type= "Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.LoggingExceptionHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" logCategory= "Error" eventId= "100" severity= "Error" title= "Exception Handling" formatterType= "Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.TextExceptionFormatter, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling" priority= "0" /> </exceptionHandlers> </add> </exceptionTypes> </add> <add name= "Log Only Policy" > <exceptionTypes> <add name= "All Exceptions" type= "System.Exception, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" postHandlingAction= "None" > <exceptionHandlers> <add name= "Logging Exception Handler" type= "Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.LoggingExceptionHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" logCategory= "Error" eventId= "100" severity= "Error" title= "Exception Handling" formatterType= "Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.TextExceptionFormatter, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling" priority= "0" /> </exceptionHandlers> </add> </exceptionTypes> </add> </exceptionPolicies> </exceptionHandling> <system.net> <defaultProxy useDefaultCredentials= "true" > <proxy usesystemdefault= "False" proxyaddress= "http://proxyip:80" bypassonlocal= "True" /> </defaultProxy> </system.net> </configuration> |
AppLogger.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | public static class AppLogger { private static Object staticLock = new object (); static AppLogger() { Logger.SetLogWriter( new LogWriterFactory().Create()); IConfigurationSource config = ConfigurationSourceFactory.Create(); ExceptionPolicyFactory factory = new ExceptionPolicyFactory(config); ExceptionManager exManager = factory.CreateManager(); ExceptionPolicy.SetExceptionManager(factory.CreateManager()); } public static void LogError(Exception ex) { lock (staticLock) { ExceptionPolicy.HandleException(ex, AppConfiguration.ExceptionPolicyName); } } public static void LogErrorOnly(Exception ex) { lock (staticLock) { try { ExceptionPolicy.HandleException(ex, AppConfiguration.LogOnlyPolicyName); } catch (Exception excep) { LogHelper.LogInfo(excep.Message); } } } public static bool HandleException(Exception ex, string exceptionPolicyName) { lock (staticLock) { return ExceptionPolicy.HandleException(ex, exceptionPolicyName); } } public static void Write( object message) { lock (staticLock) { Logger.Write(message); } } public static void Write( object message, string category) { lock (staticLock) { Logger.Write(message, category); } } public static void Write( object message, TraceEventType severity) { lock (staticLock) { LogEntry entry = new LogEntry(); entry.Message = message == null ? "" : message.ToString(); entry.Severity = severity; Logger.Write(entry); } } public static void Debug( object message) { lock (staticLock) { string msg = string .Format( "[{0:yyyy/MM/dd HH:mm:ss.fff}] {1}" , DateTime.Now, message); try { System.Diagnostics.Debug.WriteLine(msg); } catch (Exception ex) { LogErrorOnly( new ApplicationException( string .Format( "Could not write debug message: {0}" , msg), ex)); } } } } |
LogFormatHelper.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 | public class LogFormatHelper { public static string GetMethodDetail(MethodBase method){ // MethodBase.GetCurrentMethod().Name; StringBuilder methodDetail = new StringBuilder(method.Name); ; ParameterInfo[] paras = method.GetParameters(); StringBuilder parasStr = new StringBuilder(); foreach (ParameterInfo para in paras) { parasStr.Append( string .Format( "{0} {1}," ,para.ParameterType.Name.ToString(),para.Name)); } if ( paras.Length > 0) parasStr.Remove(parasStr.Length - 1, 1); methodDetail.Append( "(" ).Append(parasStr.ToString()).Append( ")" ); return methodDetail.ToString(); } public static Dictionary< string , string > GetLogInfo( string methodName) { Dictionary< string , string > dics = new Dictionary< string , string > { { "MethodName" , methodName }, { "SwitchUserPermission" , WebHelper.SwitchUserPermission.ToString() }, { "FactoAd" , WebHelper.FactoAd }, { "CurrentAd" , WebHelper.CurrentAd }, { "TM" , WebHelper.CurrentTm } }; return dics; } public static Dictionary< string , string > GetLogInfo( string methodName, TimeSpan elapsed) { Dictionary< string , string > dics = GetLogInfo(methodName); dics.Add( "RequestElapsed" , elapsed.ToString()); return dics; } public static void LogServiceError(Exception ex, string methodName, params object [] objs) { try { Dictionary< string , string > dics = LogFormatHelper.GetLogInfo(methodName); LogServiceError(ex, dics,objs); } catch (Exception innerExp) { innerExp.Data.Add( "Logger error" , innerExp.Message); AppLogger.LogError(innerExp); } } public static void LogServiceError(Exception ex, string methodName, TimeSpan elapsed, params object [] objs) { try { Dictionary< string , string > dics = LogFormatHelper.GetLogInfo(methodName, elapsed); LogServiceError(ex, dics, objs); } catch (Exception innerExp) { innerExp.Data.Add( "Logger error" , innerExp.Message); AppLogger.LogError(innerExp); } } public static void LogServiceError(Exception ex, Dictionary< string , string > dics, params object [] objs) { try { AddParametersToError(ex, dics , objs); AppLogger.LogError(ex); } catch (Exception innerExp) { innerExp.Data.Add( "Logger error" , innerExp.Message); AppLogger.LogError(innerExp); } } public static void LogFunction( string functionName) { try { AppLogger.Write(functionName); } catch (Exception innerExp) { innerExp.Data.Add( "Logger error" , innerExp.Message); AppLogger.LogError(innerExp); } } public static void LogFunction( string functionName, TimeSpan elapsed) { try { StringBuilder sb = new StringBuilder(); sb.AppendFormat( "FunctionName:{0}\r\nFunctionElapsed:{1}\r\n" , functionName, elapsed); AppLogger.Write(sb); } catch (Exception innerExp) { innerExp.Data.Add( "Logger error" , innerExp.Message); AppLogger.LogError(innerExp); } } public static void LogRequestParams( string methodName, params object [] objs) { try { Dictionary< string , string > dics = GetLogInfo(methodName); LogRequestParams(dics, objs); } catch (Exception innerExp) { innerExp.Data.Add( "Logger error" , innerExp.Message); AppLogger.LogError(innerExp); } } public static void LogRequestParams( string methodName, TimeSpan elapsed , params object [] objs) { try { Dictionary< string , string > dics = GetLogInfo(methodName, elapsed); LogRequestParams(dics, objs); } catch (Exception innerExp) { innerExp.Data.Add( "Logger error" , innerExp.Message); AppLogger.LogError(innerExp); } } public static void LogRequestParams(Dictionary< string , string > dics, params object [] objs) { try { StringBuilder sb = new StringBuilder(); foreach ( var dic in dics) { sb.AppendFormat( "{0} = {1}" , dic.Key, dic.Value).AppendLine(); //LogRequestParams(obj); } sb.Append( "Other objects =" ).AppendLine(); foreach ( object obj in objs) { LogRequestParams(sb ,obj); } AppLogger.Write(sb.ToString()); } catch (Exception innerExp) { innerExp.Data.Add( "Logger error" , innerExp.Message); AppLogger.LogError(innerExp); } } public static void LogRequestParams(StringBuilder sb, object obj) { try { Dictionary< string , object > result = BuildParametersDictionary(obj, "" ); // StringBuilder sb = new StringBuilder(); // if (elapsed != null) sb.AppendFormat("Request Elapsed: {0}", elapsed).AppendLine(); sb.AppendFormat( "{0}:" , obj.GetType().Name).AppendLine(); foreach ( var p in result) { sb.AppendFormat( "{0} = {1}" , p.Key, p.Value).AppendLine(); } } catch (Exception innerExp) { innerExp.Data.Add( "Logger error" , innerExp.Message); AppLogger.LogError(innerExp); } } public static void AddParametersToError(Exception ex, Dictionary< string , string > dics , params object [] objs) { foreach ( var dic in dics) { ex.Data.Add(dic.Key, dic.Value); } foreach ( object obj in objs) { AddParametersToError(ex,obj, "" ); } } public static void AddParametersToError(Exception ex, object obj, string propertyNamePrefix) { if (obj == null ) return ; Dictionary< string , object > parameters = BuildParametersDictionary(obj, propertyNamePrefix); foreach ( var p in parameters) { ex.Data.Add(p.Key, p.Value); } } private static Dictionary< string , object > BuildParametersDictionary( object obj, string propertyNamePrefix) { Dictionary< string , object > result = new Dictionary< string , object >(); if (obj == null ) return result; if (obj is string || obj is DateTime || obj is Boolean) { result.Add(obj.GetType().ToString(), obj.ToString()); return result; } var properties = obj.GetType().GetProperties(); foreach (PropertyInfo p in properties) { Type pType = p.GetType(); object pValue = p.GetValue(obj, null ); if (pValue == null ) { string pName = string .IsNullOrEmpty(propertyNamePrefix) ? p.Name : propertyNamePrefix + "." + p.Name; result.Add(pName, "null" ); } else if (pType.IsValueType || pValue is string || pValue is DateTime || pValue is Boolean) { string pName = string .IsNullOrEmpty(propertyNamePrefix) ? p.Name : propertyNamePrefix + "." + p.Name; if (p.Name.ToLower() == "password" || p.Name.ToLower() == "dateofbirth" || p.Name.ToLower() == "encrykey" ) result.Add(pName, "******" ); else result.Add(pName, pValue); } else if (pType.FullName.Contains( typeof (List<>).FullName)) { var list = obj as System.Collections.ICollection; if (list != null && list.Count > 0) { int index = 0; foreach ( object objInList in list) { if (objInList is ValueType || objInList is string ) result.Add(p.Name + "[" + index.ToString() + "]" , objInList); else { Dictionary< string , object > childResult = BuildParametersDictionary(objInList, p.Name + "[" + index.ToString() + "]" ); foreach ( var e in childResult) { result.Add(e.Key, e.Value); } } index++; } } } else if (pValue.GetType().FullName.Contains( typeof (List<>).FullName)) { var list = pValue as System.Collections.ICollection; if (list != null && list.Count > 0) { int index = 0; foreach ( object objInList in list) { if (objInList is ValueType || objInList is string ) result.Add(p.Name + "[" + index.ToString() + "]" , objInList); else { Dictionary< string , object > childResult = BuildParametersDictionary(objInList, p.Name + "[" + index.ToString() + "]" ); foreach ( var e in childResult) { result.Add(e.Key, e.Value); } } index++; } } } } return result; } } |
string methodName = LogFormatHelper.GetMethodDetail(MethodBase.GetCurrentMethod());
LogFormatHelper.LogRequestParams(methodName, objectname);
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!