score:13
you can call tostring() on the documentdb query to get the sql translation of the linq expression that's sent over the wire.
string sql = client.createdocumentquery<mytype>(collectionuri).where(t => t.name = "x").tostring();
// sql is somthing like select * from c where c["name"] = "x"
score:0
the accepted answer is kind of weird : it means that you have to log each and every piece of linq code you made.
(using aspnetcore di).
knowing that the microsoft.azure.cosmos.cosmosclient
contains an httpclient
, the first thing we can do is search if we can pass our own httpclient
.
and we can. for example, via cosmosclientbuilder
and using the injected ihttpclientfactory
:
.withhttpclientfactory(() => httpclientfactory.createclient("client"))
if your log filters are well configured, you'll see the request / response in the console.
now, we could either add a delegatinghandler
to our httpclient
this way in configureservices
or builder.services
in .net 6 :
services.addtransient<nosqlloggingdelegatinghandler>();
services.addhttpclient("client")
.addhttpmessagehandler<nosqlloggingdelegatinghandler>();
or find out if cosmosclient
has the option to add our own, and of course it does (example via cosmosclientbuilder
).
.addcustomhandlers(
new nosqlloggingdelegatinghandler(loggerfactory.createlogger<nosqlloggingdelegatinghandler>()))
with this, we can intercept the request and response sent :
public override async task<responsemessage> sendasync(requestmessage request, cancellationtoken cancellationtoken)
{
_logger.loginformation("requesting {uri}.\n{query}",
request.requesturi, await getqueryasync(request.content));
responsemessage response = await base.sendasync(request, cancellationtoken);
_logger.loginformation("requested {uri} - {status}",
response.requestmessage.requesturi, response.statuscode);
return response;
}
and getqueryasync
reads the request's body stream using system.text.json
:
private static async valuetask<string?> getqueryasync(stream content)
{
string? stringcontents;
// create a streamreader with leaveopen = true so it doesn't close the stream when disposed
using (streamreader sr = new streamreader(content, encoding.utf8, true, 1024, true))
{
stringcontents = await sr.readtoendasync();
}
content.position = 0;
if (string.isnullorempty(stringcontents))
{
return null;
}
try
{
using jsondocument parsedjobject = jsondocument.parse(stringcontents);
return parsedjobject.rootelement.getproperty("query").getstring();
}
catch (keynotfoundexception)
{
return stringcontents;
}
// not a json.
catch (jsonexception)
{
return stringcontents;
}
}
and there you have it. i've tried to shortened the code, for example, i didn't add a condition to check if the logging level is enabled or not, to avoid doing useless work.
score:5
you could just use fiddler and view the json of the request after it is sent.
view an example here:
Source: stackoverflow.com
Related Query
- How can I trace the query produced by the documentdb Linq provider?
- How can I write the following code more elegantly using LINQ query syntax?
- How can I check the number of calls to the database in LINQ query when using .NET Core and Code First?
- How can I view the Entity Framework LINQ query plan cache?
- How can I speed up this linq query on a List<> with search criteria on 3 attributes in the list object
- How can I code a Linq query to do an upward Include?
- How can I query the contents of a List via LINQ based on string values?
- How can we express the following code using query expression?
- How can I access the group of a linq group-by query from a nested repeater control?
- How can I make a LINQ query with subqueries in the from statement?
- How can i change this LINQ query to use my List<int> instead of the hard coded number?
- How Can I Write a LINQ Query to Get Content Within the Last Seven Days?
- How do I determine the source of unpredictable LINQ query results?
- How can I use a predicate in a crm sdk 2011 linq query when the query contains a join?
- How can I code numerous MIN functions into one LINQ to DataSet query
- How can I do a LINQ query joining two entities and select what a Where on one of the entities?
- How can I dynamically build a linq OR query where one of several columns can match the search string?
- How can I keep the the logic to translate a ViewModel's values to a Where clause to apply to a linq query out of My Controller?
- How can I get the type of the results of a LINQ query before executing it?
- Can I Use The Same Linq Code to Query for Different Conditions?
- How can i change the following LINQ query as normal sql query
- How to write aggregate query in LINQ reusing part of the select code
- How to execute code as a part of the LINQ query
- How can I build a LINQ predicate/dynamic.LINQ query based off grid filtering when the grid properties don't have the entity properties?
- How can I condense the following Linq query into one IQueryable
- linq lamba how can i query a list of child properties and return the parent
- How can I specify the range of rows my LINQ query should retrieve
- How can I query a database with LINQ when the column is a number but I have a string in the WHERE?
- How can I reformat this to use the other kind of LINQ Query format?
- How can I order the output of my long LINQ query by a double and then a string?
More Query from same tag
- How can i achieve this in LINQ-Queries?
- Am I doing something wrong from a connection pooling point of view?
- LINQ SQL query, SubmitChanges is not submitting changes to DB
- LINQ find records with no join
- Create generic array in VB.NET
- Does IEnumerable.FirstOrDefaultAsync( predicate )?.ContinueWith() return if the result is null?
- simple way to read xml data using linq
- Iterating through datacontext - most efficient way?
- Entity Framework linq query to find records with turkish characters
- Simple LINQ question - how to iterate through a group?
- Convert Linq to SQL
- Fast way to put 2 lists of String arrays into a DataTable?
- Group dictionaries with Linq
- Read xml using LINQ and store in a list of objects
- How to implement search functionality when there is 3 texboxes and one search button in mvc 4
- LINQ error - The method 'Sum' is not supported
- LINQ: is there a way to conditionally modify expression?
- how to query a sub object of an object with linq
- how can i return model property from lambda expression (like mvc's "html.textboxfor(yyy)")?
- Linq equivalent of aggregate function on multiple tables in one database trip
- C# linq finding all string values in a list there match in mongodb
- Calculate polynomial using LINQ
- LINQ nested select
- Write sql query to linq
- How to Search through all fields in a LINQ table?
- Getting wrong output with linq join query
- How to create linq predicate with multiple nested "ands" and "ors"
- Find or Create Element in LINQ-to-XML
- add dummy column in LINQ fetch query
- Sorting all the XmlNode in XmlDocument