AOP Logging with custom arguments - c#

Before every method executes, I want to log all parameters from that method. For that, I would use AOP (Aspect oriented programming) and create a "Loggable" attribute. But... I also would like to know the content of my parameters. For a string-parameter, that's not a problem. But I would like to know a good solution for every occasion, like objects, ints, doubles,...
Is there any other way to solve this other then to overwrite the ToString() method?
(in examples I always came across a string parameter...)
Example:
public class MyType {
public int Id { get; set; }
public string Name { get; set; }
public double Value { get; set; }
}
[Loggable]
public void IDoWhateverIWant(MyType data, int count, string message){
//Whatever I want
}
IDoWhateverIWant(
new MyType(){
Id = 1,
Name = "My test data name",
Value = 1234.56}
, 5
, "Log me, please");
Logmessage (the way these parameters are presented are the same to me, I just want to see them):
22/07/2014 9:30 Incoming message: IDoWhateverIWant with parameters:
- data : Id = 1
Name = "My test data name"
Value = 1234.56
- count: 5
- message: "Log me, please"

A simple option is to use NewtonSoft.Json to serialize each parameter. This simple example:
public void IDoWhateverIWant(MyType data, int count, string message)
{
string logMessage =
ParameterValueMessage(data, "data") + Environment.NewLine +
ParameterValueMessage(count, "count") + Environment.NewLine +
ParameterValueMessage(message, "message");
Debug.Print(logMessage);
}
public static string ParameterValueMessage<T>(T arg, string name)
{
string result = JsonConvert.SerializeObject(arg, Formatting.Indented);
return name + ": " + result;
}
Produces the following:
data: {
"Id": 1,
"Name": "My test data name",
"Value": 1234.56
}
count: 5
message: "Log me, please"
My own preference though is for each method to take a single parameter object - the logging code is a lot neater and more generic with parameter objects and all of the individual parameter names can be inferred from the parameter object:
public class MyParameterObject
{
public MyType data { get; set; }
public int count { get; set; }
public string message { get; set; }
}
which is serialized in much the same way and has the benefit of not having to pass in the name of every individual parameter:
public void IAlsoDoWhateverIWant(MyParameterObject parameterObject)
{
string logMessage = ParameterValueMessage(parameterObject);
Debug.Print(logMessage);
}
public static string ParameterValueMessage<T>(T arg)
{
return JsonConvert.SerializeObject(arg, Formatting.Indented);
}
and produces the following output:
{
"data": {
"Id": 1,
"Name": "My test data name",
"Value": 1234.56
},
"count": 5,
"message": "Log me, please"
}

Related

Unable to handle Collection Deserealization Error

I'm trying to handle deresealize an object that does not comply with some of my classes. I wold like the code to execute and fail only on the invalid attributes but the deserealization method is returning a null Object.
I am using this method in a generic utility class that deserealizes some string to any given type.
From the test code, the error handler works correctly on invalid dates and other invalid types and returns the object with the default .NET initialization values.
If I change (or comment) the Items collection in the sub object, the code works.
string json = "{\"Id\":8,\"CreatedByUserId\":0,\"CreatedOnDate\":\"2019X-Y02Z-W06T18:A51:05.783\",\"LastModifiedByUserId\":1,\"LastModifiedOnDate\":\"2019-03-12T17:00:34.82\",\"OperationData\":{\"IsActive\":true,\"Items\":[{\"_Id\":1,\"Id_Value\":0,\"Id\":1},{\"_Id\":2,\"Id\":2},{\"Id\":1,\"IsDeleted\":false,\"Content\":{}}]}}";
TestType test = DeserealizeContent(json);
/*The convertion utility*/
private static TestType DeserealizeContent(string data)
{
var settings = new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore,
MissingMemberHandling = MissingMemberHandling.Ignore,
Error = HandleDeserializationError
};
var r = JsonConvert.DeserializeObject<TestType>(data, settings);
return r;
}
public static void HandleDeserializationError(object sender, ErrorEventArgs errorArgs)
{
errorArgs.ErrorContext.Handled = true;
}
/*Supporting types*/
public class TestType {
public int Id { get; set; }
public DateTime CreatedOnDate { get; set; }
public int CreatedByUserId { get; set; }
public string Instructions { get; set; }
public OperationDataType OperationData {get;set;}
}
public class OperationDataType {
public bool IsActive { get; set; }
public List<int> Items { get; set; }
}
I was expecting the error handler to catch handle the exception and continue with the process but instead the deserealization just returns null in the end.
If I change List Items to List Items the result is correctly parsed.
My expected result wold be:
{
"Id": 8,
"CreatedByUserId": 0,
"CreatedOnDate": null,
"LastModifiedByUserId": 1,
"LastModifiedOnDate": "2019-03-12T17:00:34.82",
"OperationData": {
"IsActive": true,
"Items": null
}
}
EDIT - workaround
The suggestion from Yair (bellow) works.
Changing from List to List works as expected and the exception get handle correctly.
The items in your json is not an array of int so how you want it to be List?
Items is array of objects look at the json formated:
{
"Id": 8,
"CreatedByUserId": 0,
"CreatedOnDate": "2019X-Y02Z-W06T18:A51:05.783",
"LastModifiedByUserId": 1,
"LastModifiedOnDate": "2019-03-12T17:00:34.82",
"OperationData": {
"IsActive": true,
"Items": [{
"_Id": 1,
"Id_Value": 0,
"Id": 1
}, {
"_Id": 2,
"Id": 2
}, {
"Id": 1,
"IsDeleted": false,
"Content": {}
}
]
}
}
You can do one of three things:
handle the json content as string or map it so the items will be int array [1,2,3].
create item class for the items that include all the fields you need then extract the int that you want.
get it as object like you do now and use reflection () for getting the int you want.
you can use this function for reflection:
public static object GetPropValue(object src, string propName)
{
return src.GetType().GetProperty(propName).GetValue(src, null);
}
and then use it like this:
GetPropValue(test.OperationData.items[0], "Id")
EDIT
you can use this to deserialize the json in generic way:
Newtonsoft.Json.Linq.JObject jsonDeserialized = (Newtonsoft.Json.Linq.JObject)Newtonsoft.Json.JsonConvert.DeserializeObject<object>(json);
and then
you can map it manually or with automapper to the new TestType test that you want without the items in it.
you can get the values like this:
test.Id = jsonDeserialized["Id"];
test.CreatedByUserId = jsonDeserialized["CreatedByUserId"];
and so on
according to your last comment i find that if i changed the List<int> Items to List<long> Items it works as you wanted. it is something with primitive types and the parsing that deserialize do.

Newtonsoft.Json.JsonReaderException: 'Unexpected character encountered while parsing value: [.

I'm doing C# JSON <-> PHP JSON for the first time.
Thought I'd get on an easy road but seems like I've hit the rock.
I'm fairly sure that JSON from Newtonsoft allows "[" character but not sure why i have this error instead?
Here's my C# code:
public class SystemJSON
{
public bool Status { get; set; }
public string Message { get; set; }
public string ResponseData { get; set; }
}
public static class SystemCall
{
public static String Post(string uri, NameValueCollection pairs)
{
byte[] response = null;
using (WebClient wc = new WebClient())
{
response = wc.UploadValues(uri, pairs);
}
return Encoding.Default.GetString(response);
}
}
string system_Response = SystemCall.Post("http://127.0.0.1:8080/edsa-NEFS%20(PHP)/api.php", new NameValueCollection()
{
{"do_work", Functions.Get_Department_List.ToString()},
{"api_data", null }
});
**SystemJSON systemJSON = JsonConvert.DeserializeObject<SystemJSON>(system_Response);** //<-- Error happens here.
if(systemJSON.Status == true)
{
//do stuff here
}else
{
MessageBox.Show(this, systemJSON.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
And here's my PHP code:
<?php
// Load Request
$function_name = isset($_POST['do_work']) ? $_POST['do_work'] : '';
$api_data = isset($_POST['api_data']) ? $_POST['api_data'] : '';
// Validate Request
if (empty($function_name))
{
SystemResponse(false, 'Invalid Request');
}
if (!function_exists($function_name))
{
SystemResponse(false, 'API Method Not Implemented');
}
// Call API Method
call_user_func($function_name, $api_data);
/* Helper Function */
function SystemResponse($responseStatus, $responseMessage, $responseData = '')
{
exit(json_encode(array(
'Status' => $responseStatus,
'Message' => $responseMessage,
'ResponseData' => $responseData
)));
}
/* API Methods */
function Get_Department_List($api_data)
{
//Test ------------------------------------------START
$node = array();
$dept = array();
$responseData = array();
$dept['id'] = 1;
$dept['name'] = "General";
$dept['description'] = "Forms, Samples, Templates, Catalogs, etc";
$dept['status'] = 1;
array_push($node, $dept);
$dept['id'] = 2;
$dept['name'] = "Test";
$dept['description'] = "Testing";
$dept['status'] = 1;
array_push($node, $dept);
$responseData["dept"] = $dept;
SystemResponse(true, 'SUCCESS', $responseData);
//Test ------------------------------------------END
}
?>
And here's my error:
Newtonsoft.Json.JsonReaderException HResult=0x80131500
Message=Unexpected character encountered while parsing value: {. Path
'ResponseData', line 1, position 51.
The problem is that your C# SystemJSON class does not match the structure of the incoming JSON correctly.
ResponseData in your C# SystemJSON class is listed as a string but your PHP appears to be pushing out a complex object inside that property. You can't deserialise an object into a string - there is no way for the deserialiser to know how to translate the object structure into a suitable string, and anyway it's not generally a useful or logical thing to do. So instead it throws an error to say the object structure doesn't match.
The specific error you're seeing means the deserialiser is expecting a " to denote the start of a string but instead it's seeing { denoting the start of another object.
Why is this happening? Well, your PHP code will produce a JSON response which looks like this:
{
"Status": true,
"Message": "SUCCESS",
"ResponseData": {
"dept": {
"id": 2,
"name": "Test",
"description": "Testing",
"status": 1
}
}
}
Live demo here
As you can see, ResponseData contains an object, which has a "dept" which in turn is another object with four more properties.
To deserialise this properly, your SystemJSON class will need to be altered, and you'll also need two sub-classes to help it out:
public class SystemJSON
{
public bool Status { get; set; }
public string Message { get; set; }
public ResponseData ResponseData { get; set; }
}
public class ResponseData {
public Department dept {get; set; }
}
public class Department {
public string id {get; set; }
public string description {get; set; }
public int status {get; set; }
}
You will now be able to deserialise the JSON correctly. Here is a live demo of the deserialisation.
P.S the [ character appears to be irrelevant here...it's unclear why you referred to that in your question.
P.P.S. From looking at your PHP I'm guessing that you may be intending to return different data structures in ResponseData depending on which parameter was specified for do_work - i.e. depending on which PHP function is called. If so then you'll need to amend your C# accordingly so that it deserialises to a different concrete class depending on which API method it requests. Or you could possibly cheat and specify ResponseData as dynamic, which will then accept any data structure it received, albeit with the caveat that it's now effectively loosely-typed and so you lose certain benefits when compiling the code such as checking for valid usage of property names, data types etc.

When using jqgrid in asp.net-mvc site, is there a way to include the filter in the url so you can share filtered views?

I have an asp.net-mvc site and I user jqgrid on the front end. I have a simple page using jqgrid and I filter down my jqgrid results (server side filter) using the top bar filter of the advanced filter.
I now want a way where I can share a URL with someone else and when they load the page, they get the same filter applied so somehow I need to take the filter criteria and append it to the query string.
The issue is that I can do this "manually" field by field like this by creating queryparams like
myurl?NameFilter=JoeBrown
and then doing something like this in my asp.net-mvc view
var myfilter = { groupOp: "AND", rules: [] };
<% if (!String.IsNullOrEmpty(Model.NameFilter)) { %>
myfilter.rules.push({ field: "Name", op: "eq", data: "<% = Model.NameFilter%>" });
<%}%>
but that doesn't really scale very well given I have many different pages with lots of columns so I am looking for a more generic way to persist the filter values into a URL and then apply them again so that I can then model bind on the server side back to my controller action.
Here is an example Server Side controller action that I am calling to load data from the server:
public ActionResult GridData(GridData args)
{
var data = GetData(args).Paginate(args.page ?? 1, args.rows ?? 10,
i =>
new
{
i.Id,
i.Name
}
}
so basically I need the query string to bind to my GridData class similar to what happens when I do a normal filter that gets posted on the ajax call when I do a regular filter.
My GridData class looks like this:
public class GridData
{
public int? page { get; set; }
public int? rows { get; set; }
public bool search { get; set; }
public string sidx { get; set; }
public string sord { get; set; }
public Filter Where { get; set; }
}
public class Filter
{
public string groupOp { get; set; }
public Rule[] rules { get; set; }
}
public class Rule
{
public string field { get; set; }
public string op { get; set; }
public string data { get; set; }
}
If you are looking for a way to construct a model that will bind directly from query string values, you can try the following. The methods buildParamsCustom() and serializeJson() are basically taken from jQuery source and modified for creating a query string which will be supported by the MVC default model binder.
http://api.jquery.com/jquery.param/ has the details about the default jQuery implementation.
I have tested for your scenario. I am able to view the serialized data.
var r20 = /%20/g, rbracket = /\[\]$/;
function buildParamsCustom(prefix, obj, traditional, add) {
var name;
if (jQuery.isArray(obj)) {
// Serialize array item.
jQuery.each(obj, function (i, v) {
if (traditional || rbracket.test(prefix)) {
// Treat each array item as a scalar.
add(prefix, v);
} else {
// Item is non-scalar (array or object), encode its numeric index.
buildParamsCustom(prefix + "[" + (typeof v === "object" ? i : "") + "]", v, traditional, add);
}
});
} else if (!traditional && jQuery.type(obj) === "object") {
// Serialize object item.
for (name in obj) {
buildParamsCustom(prefix + "." + name, obj[name], traditional, add);
}
} else {
// Serialize scalar item.
add(prefix, obj);
}
}
// Serialize an array of form elements or a set of
// key/values into a query string
var serializeJson = function (a, traditional) {
var prefix,
s = [],
add = function (key, value) {
// If value is a function, invoke it and return its value
value = jQuery.isFunction(value) ? value() : (value == null ? "" : value);
s[s.length] = encodeURIComponent(key) + "=" + encodeURIComponent(value);
};
// Set traditional to true for jQuery <= 1.3.2 behavior.
if (traditional === undefined) {
traditional = jQuery.ajaxSettings && jQuery.ajaxSettings.traditional;
}
// If an array was passed in, assume that it is an array of form elements.
if (jQuery.isArray(a) || (a.jquery && !jQuery.isPlainObject(a))) {
// Serialize the form elements
jQuery.each(a, function () {
add(this.name, this.value);
});
} else {
// If traditional, encode the "old" way (the way 1.3.2 or older
// did it), otherwise encode params recursively.
for (prefix in a) {
buildParamsCustom(prefix, a[prefix], traditional, add);
}
}
// Return the resulting serialization
return s.join("&").replace(r20, "+");
};
// Get this data from the grid
var data = { "page": 1, "rows": 10, "search": true, "Where": { "groupop": "AND", "rules": [{ "field": "Name", "op": "EQ", data: "John" }, { "field": "Title", "op": "EQ", data: "Mr" }] } };
var queryString = serializeJson(data);
var url = "someurl" + "?" + decodeURIComponent(queryString);
// Send your GET request here.

How to Deserialize a JSON array in List (C#)

I am struggling with a subject that has a lot of variants in this forum but I can't seem to find one that suits me, and I think it's because of the way that my JSON array is :(
I'm not an expert but I already manage to "almost" get the end...
I need to get hand in "Success" and "Status" value. But also the different "Addresses".
My JSON (is called responseFromServer):
{
"success":true,
"addresses":
[
{"DPID":658584,"SourceDesc":"Postal\\Physical","FullAddress":"1/8 Jonas Street, Waimataitai, Timaru 7910"},
{"DPID":658585,"SourceDesc":"Postal\\Physical","FullAddress":"2/8 Jonas Street, Waimataitai, Timaru 7910"},
{"DPID":658583,"SourceDesc":"Postal\\Physical","FullAddress":"3/8 Jonas Street, Waimataitai, Timaru 7910"}
],
"status":"success"
}
Then, based on lot of examples in this forum, taking bits and pieces I created my classes:
public class jsonDataTable
{
public bool success { get; set; }
public IEnumerable<dtaddresses> addresses { get; set; }
public string status { get; set; }
}
public class dtaddresses
{
public int DPID { get; set; }
public string SourceDesc { get; set; }
public string FullAddress { get; set; }
}
Then I'm going to Deserialize:
public void _form_OnCallingAction(object sender, ActionEventArgs e)
{
...
...
JavaScriptSerializer js = new JavaScriptSerializer();
jsonDataTable jsonArray = js.Deserialize<jsonDataTable>(responseFromServer);
...
string tb = jsonArray.status.ToString();
string tb2 = jsonArray.success.ToString();
...
...
List<dtaddresses> _listAddresses = new List<dtaddresses>
{
new dtaddresses()
};
...
...
try
{
string tb3 = _listAddresses.Count.ToString();
string tb4 = _listAddresses[0].FullAddress;
}
catch (Exception ex)
{
CurrentContext.Message.Display(ex.Message + ex.StackTrace);
}
...
...
...
CurrentContext.Message.Display("Raw Response from server is: {0}", responseFromServer);
//Returns all the content in a string to check. OK! :)
CurrentContext.Message.Display("The success value is: {0} ", tb);
//Returns the Status Value (in this case "success") OK! :)
CurrentContext.Message.Display("The status value is: {0} ", tb2);
//Returns the Success Value (in this case "true") giggity giggity! All Right! :)
CurrentContext.Message.Display("The n. of addresses is: {0} ", tb3);
//Returns how many addresses ( in this case is returning 0) not ok... :(
CurrentContext.Message.Display("The address value is: {0} ", tb4);
// Returns the Fulladdress in index 0 (in this case nothing...) not ok... :(
Can any one help me to understand why I can access the values in the "dtaddresses" class?
This is the far that I went...
The following piece of code I copied from your question is creating a brand new list that has nothing to do with your deserialized data. Thus it's always going to be a single element list, where the first element contains only default values, which is what you are seeing in tb3 and tb4 later on.
List<dtaddresses> _listAddresses = new List<dtaddresses>
{
new dtaddresses()
};
Instead, assign jsonArray.addresses to _listAddresses, such as:
List<dtaddresses> _listAddresses = jsonArray.addresses.ToList()
Or you can forget about _listAddresses completely, and just simply reference jsonArray.addresses directly, such as:
string tb3 = jsonArray.addresses.Count().ToString();
string tb4 = jsonArray.addresses.First().FullAddress;

Dictionary of Objects doesn't work as JSON

I have spent WAY too much time trying to figure out how to pull all the values I need to from my C# application using JS and JSON. It works fine when I just use simple structures, such as an array, but I need to be able to grow the list at runtime.
Right now, the best I could figure out was doing a Dictionary with an incrementing key value, and the other 3 values as a class object. However, this seems to crash out my C# application.
What would be the best way to do this?
Relevant C# Code:
public class ChatData
{
string userName;
string message;
System.DateTime timestamp;
public ChatData(string name, string msg)
{
userName = name;
message = msg;
timestamp = System.DateTime.Now;
}
}
else if (string.Equals(request, "getchat"))
{
//string since = Request.Query.since;
Dictionary<int, ChatData> data = new Dictionary<int, ChatData>();
data.Add(1, new ChatData("bob", "hey guys"));
data.Add(2, new ChatData("david", "hey you"));
data.Add(3, new ChatData("jill", "wait what"));
return Response.AsJson(data);
}
Relevant Javascript:
function getChatData()
{
$.getJSON(dataSource + "?req=getchat", "", function (data)
{
//$.each(data, function(key, val)
//{
//addChatEntry(key, val);
//})
});
}
You haven't explained what Response.AsJson is and how it is implemented but if it uses JavaScriptSerializer you will get the following exception:
Unhandled Exception: System.ArgumentException: Type
'System.Collections.Generic. Dictionary`2[[System.Int32, mscorlib,
Version=4.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089],[ChatData, Test, Version=1.0.0.0,
Culture=neutral, PublicKeyToken=null]]' is not supported for
serialization/deserialization of a dictionary, keys must be strings or
objects.
which is pretty self-explanatory. You cannot use integers as keys if you intend to JSON serialize this structure. Also because your ChatData class no longer has a default/parameterless constructor you won't be able to deserialize a JSON string back to this class (but I guess you don't need this yet).
So one possible solution to your problem would be to use:
Dictionary<string, ChatData> data = new Dictionary<string, ChatData>();
data.Add("1", new ChatData("bob", "hey guys"));
data.Add("2", new ChatData("david", "hey you"));
data.Add("3", new ChatData("jill", "wait what"));
Now of course this being said and looking at the javascript you commented out and what you intend to do, as I already explained you in your previous question, dictionaries are not serialized as javascript arrays, so you cannot loop over them.
Long story short, define a class:
public class ChatData
{
public string Username { get; set; }
public string Message { get; set; }
public DateTime TimeStamp { get; set; }
}
and then fill an array of this class:
var data = new[]
{
new ChatData { Username = "bob", Message = "hey guys" },
new ChatData { Username = "david", Message = "hey you" },
new ChatData { Username = "jill", Message = "wait what" },
};
return Response.AsJson(data);
and finally consume:
$.getJSON(dataSource, { req: 'getchat' }, function (data) {
$.each(data, function(index, element) {
// do something with element.Username and element.Message here, like
$('body').append(
$('<div/>', {
html: 'user: ' + element.Username + ', message:' + element.Message
})
);
});
});
Why not simply use a typed list? Also, you'll need a default constructor to serialize/deserialize it. Note how I've modified your class to use properties
as well. Note, as #rudolf_franek mentions, you can add an ID property to the ChatData class if you need to be able to link to it.
public class ChatData
{
public ChatData()
{
TimeStamp = DateTime.Now;
}
public int ID { get; set; }
public string Who { get; set; }
public string Said { get; set; }
public DateTime TimeStamp { get; set; }
}
...
var data = new List<ChatData>
{
new ChatData { ID = 1, Who = "bob", Said = "hey guys" },
...
};
return Response.AsJson( data );

Categories

Resources