I have a helper method that accepts parameters with generic types. This method can throw exceptions, which I log in order to debug later. Some objects have ids, some have names, etc., that would be useful to know when trying to debug. How can I log this information in a useful way?
How to identify generic objects in exception logs?
179 Views Asked by Jason At
2
There are 2 best solutions below
0
Grax32
On
This function returns a C# style type name (something like List<String> or List<Dictionary<Int32,String>>) from a type.
public static string FancyTypeName(Type type)
{
var typeName = type.Name.Split('`')[0];
if (type.IsGenericType)
{
typeName += string.Format("<{0}>", string.Join(",",
type.GetGenericArguments().Select(v => FancyTypeName(v)).ToArray())
);
}
return typeName;
}
Related Questions in C#
- Passing arguments to main in C using Eclipse
- kernel module does not print packet info
- error C2016 (C requires that a struct or union has at least one member) and structs typedefs
- Drawing with ncurses, sockets and fork
- How to catch delay-import dll errors (missing dll or symbol) in MinGW(-w64)?
- Configured TTL for A record(s) backing CNAME records
- Allocating memory for pointers inside structures in functions
- Finding articulation point of undirected graph by DFS
- C first fgets() is being skipped while the second runs
- C std library don't appear to be linked in object file
- gcc static library compilation
- How to do a case-insensitive string comparison?
- C programming: Create and write 2D array of files as function
- How to read a file then store to array and then print?
- Function timeouts in C and thread
Related Questions in GENERICS
- Implementing Iterator for abstractCollection
- C# check if there is an overload method with the specific type
- instantiating a generic class data type known at runtime
- How to tell Java that two wildcard types are the same?
- C++ Templates with multiple constraints
- How are the generic functions and types stored in an rlib?
- Java Generics missunderstanding
- Generic webAPI method based on parameter types of arrays
- C# pass generic type as a generic type parameter?
- Return int in method supposed to return generic class
- StarUML Class Diagram : How to manually add Generics (Template Type T) for a class
- Modeling an XML hierarchy for traversal with MVVM
- How to get the values of generic array in Java?
- Swift: converting between Arrays of 'Protocol' and Arrays of implementing Class
- instantiating a generic referance class with data types known at runtime
Related Questions in LOGGING
- Is Log4j2 xml configuration case sensitive?
- Logback stopped logging after splitting shared config file
- logging setup best practices
- C Simple Logging Management
- OpenShift Pyramid logging to file
- Log of dependency does not show
- Node/Express access logger from request object
- How does one locate all git log messages in the git object database?
- Logging error when executing Maven SonarQube plugin
- refactor 'execute and log' pattern
- CMD specifying columns to save?
- How to get information about error from HttpContext in WCF services
- Django not logging all errors
- Empty space at beginning of rsyslog log file
- Avoid log trace of external framework J2EE
Related Questions in CUSTOM-EXCEPTIONS
- Custom Exception: customize message before call the base constructor
- How to identify generic objects in exception logs?
- Unit test a custom Exception
- Can we throw a custom exception from catch block?
- Throwing custom Exceptions, if for loop
- Custom exceptions in Elasticsearch Nest
- Apache camel: Adding parameters to throwException statement
- How to get content type of the response in JAX-RS ExceptionMapper
- How to manage errors with bokeh library (Python + JavaScript) and show them on the user interface?
- Using exception filtering in c# 6.0 (with typeof()) VS. catching custom exception
- Logging user-defined exception C#
- Why doesn't rails 2.1 catch exceptions with rescue_from?
- Exceptions in PHP. How to use them? How to extend 'em?
- How do I make config.exceptions_app work with rspec
- Set faultCode to SENDER in a custom SoapFault?
Trending Questions
- UIImageView Frame Doesn't Reflect Constraints
- Is it possible to use adb commands to click on a view by finding its ID?
- How to create a new web character symbol recognizable by html/javascript?
- Why isn't my CSS3 animation smooth in Google Chrome (but very smooth on other browsers)?
- Heap Gives Page Fault
- Connect ffmpeg to Visual Studio 2008
- Both Object- and ValueAnimator jumps when Duration is set above API LvL 24
- How to avoid default initialization of objects in std::vector?
- second argument of the command line arguments in a format other than char** argv or char* argv[]
- How to improve efficiency of algorithm which generates next lexicographic permutation?
- Navigating to the another actvity app getting crash in android
- How to read the particular message format in android and store in sqlite database?
- Resetting inventory status after order is cancelled
- Efficiently compute powers of X in SSE/AVX
- Insert into an external database using ajax and php : POST 500 (Internal Server Error)
Popular Questions
- How do I undo the most recent local commits in Git?
- How can I remove a specific item from an array in JavaScript?
- How do I delete a Git branch locally and remotely?
- Find all files containing a specific text (string) on Linux?
- How do I revert a Git repository to a previous commit?
- How do I create an HTML button that acts like a link?
- How do I check out a remote Git branch?
- How do I force "git pull" to overwrite local files?
- How do I list all files of a directory?
- How to check whether a string contains a substring in JavaScript?
- How do I redirect to another webpage?
- How can I iterate over rows in a Pandas DataFrame?
- How do I convert a String to an int in Java?
- Does Python have a string 'contains' substring method?
- How do I check if a string contains a specific word?
Regardles if it is a generic method or not, unless your object is null, you can log the type with
myObject.GetType().ToString()EDIT:
If you would like to read more information from the object then you can read object properties like this:
This should work without any problems for primitives. For Objects it depends on how the ToString method is implemented. Also note that it will not read recursively, just the direct members of the class.