C# stack trace with variable values

  • Thread starter Thread starter NavyIsland
  • Start date Start date
N

NavyIsland

Guest
I have a winforms application that i am developing that needs to do some logging. In the event that we connect to our database we need to log a number of things one of which is a stacktrace just in case something goes wrong e.g.:

7/14/2020 10:02:31 AM
Success writing to the FM database, wrote PUDO request for the Edgebander
at System.Environment.GetStackTrace(Exception e, Boolean needFileInfo)
at System.Environment.get_StackTrace()
at ModbusServer__Katalyn_.logger.log(String logMessage) in C:\Users\Ben deVries\source\repos\ModbusServer (Katalyn)\ModbusServer (Katalyn)\logger.cs:line 46
at ModbusServer__Katalyn_.subscriptionFunctions.pudo.edgebander() in C:\Users\Ben deVries\source\repos\ModbusServer (Katalyn)\ModbusServer (Katalyn)\subscriptionFunctions.cs:line 27
at ModbusServer__Katalyn_.ModbusServer.coilsChanged(Int32 coil, Int32 count) in C:\Users\Ben deVries\source\repos\ModbusServer (Katalyn)\ModbusServer (Katalyn)\ModbusServer.cs:line 160
at ModbusServer.WriteSingleCoil(ModbusProtocol receiveData, ModbusProtocol sendData, NetworkStream stream, Int32 portIn, IPAddress ipAddressIn)
at ModbusServer.CreateAnswer(ModbusProtocol receiveData, ModbusProtocol sendData, NetworkStream stream, Int32 portIn, IPAddress ipAddressIn)
at ModbusServer.ProcessReceivedData(Object networkConnectionParameter)
at TCPHandler.ReadCallback(IAsyncResult asyncResult)
at System.Net.LazyAsyncResult.Complete(IntPtr userToken)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Net.ContextAwareResult.Complete(IntPtr userToken)
at System.Net.LazyAsyncResult.ProtectedInvokeCallback(Object result, IntPtr userToken)
at System.Net.Sockets.BaseOverlappedAsyncResult.CompletionPortCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* nativeOverlapped)
at System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* pOVERLAP)

I am hoping to be able to get the values of the variables passed into the functions (specifically my functions). and be able to get something like this:

7/14/2020 10:02:31 AM
Success writing to the FM database, wrote PUDO request for the Edgebander
at System.Environment.GetStackTrace(Exception e, Boolean needFileInfo)
at System.Environment.get_StackTrace()
at ModbusServer__Katalyn_.logger.log(some string data) in C:\Users\Ben deVries\source\repos\ModbusServer (Katalyn)\ModbusServer (Katalyn)\logger.cs:line 46
at ModbusServer__Katalyn_.subscriptionFunctions.pudo.edgebander() in C:\Users\Ben deVries\source\repos\ModbusServer (Katalyn)\ModbusServer (Katalyn)\subscriptionFunctions.cs:line 27
at ModbusServer__Katalyn_.ModbusServer.coilsChanged(some int, some int) in C:\Users\Ben deVries\source\repos\ModbusServer (Katalyn)\ModbusServer (Katalyn)\ModbusServer.cs:line 160
at ModbusServer.WriteSingleCoil(some protocol, some protocol, some network stream, some int, some IP address)
at ModbusServer.CreateAnswer(some protocol, some protocol, some network stream, some int, some IP address)
at ModbusServer.ProcessReceivedData(some object)
at TCPHandler.ReadCallback(some IAsyncResult)
at System.Net.LazyAsyncResult.Complete(IntPtr userToken)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Net.ContextAwareResult.Complete(IntPtr userToken)
at System.Net.LazyAsyncResult.ProtectedInvokeCallback(Object result, IntPtr userToken)
at System.Net.Sockets.BaseOverlappedAsyncResult.CompletionPortCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* nativeOverlapped)
at System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* pOVERLAP)

I don't care about values to or from code that is part of the .NET library. My code is very deterministic, as such knowing the variables if something fails will allow us to fix whatever issue may arise.

Any clue how to do this?
Is there some param i can pass into the stacktrace function to get these?
I have heard that System.Reflection might be useful, is that the case?


Thanks in advance,
Ben

Continue reading...
 

Similar threads

Back
Top