ASP.NET MVC Tracing Issues - asp.net-mvc

Question
How do I get ASP.NET MVC trace information to be consistent for in-page trace output as trace.axd? I may just be missing something obvious, please call it out if you see it.
Background Info for Traditional ASP.NET
So back in the regular ASP.NET days, you could simply add the following to your web.config:
<system.diagnostics>
<trace>
<listeners>
<add name="WebPageTraceListener" type="System.Web.WebPageTraceListener, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
</listeners>
</trace>
</system.diagnostics>
...
<system.web>
<trace enabled="true" pageOutput="true" writeToDiagnosticsTrace="true"/>
...
<system.codedom>
<compilers>
<compiler language="c#;cs;csharp" extension=".cs" warningLevel="1" compilerOptions="/d:TRACE" type="Microsoft.CSharp.CSharpCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
Then you could add the following in your pages:
HttpContext.Current.Trace.Write("Write Trace Here");
or
System.Diagnostics.Trace.Write("Write Trace Here");
And if you hit your page (i.e. localhost:61115/Default.aspx), you would get a nice trace table with your custom trace embedded with asp.net page events:
aspx.page Begin Load 0.00343 0.000357
test 0.00462176 0.001192
aspx.page End Load 0.00526904 0.000018
Hitting localhost:61115/Trace.axd?id=0 would retain the same trace results as the in-page trace outputs.
Background Info on ASP.NET MVC
Unfortunately, I cannot get this to work in ASP.NET MVC 2.0 for reasons unknown to me. I use similar web.config settings as ones listed above. What is interesting is that I can only get traces to partially work. So if I hit the functionality equivalent default page (aka index action method of my homecontroller), I see all of the traditional asp.net page events such as preinit, preload, prerender, etc but no custom trace msg using either System.Diagnostics nor HttpContext.Trace.Write.
However, if I turn to the Trace.axd?id=0 file, I'm greeted with my custom trace messages but no ASP.NET page event trace output. I must be missing something here that is causing this inconsistency in trace information I see in-page vs. trace.axd (recall that traditional asp.net outputs identical in-page vs. trace.axd outputs). I would really like to have my in-page trace information consistent with trace.axd (either by removing the traditional asp.net page events or other means). Is there something I am missing?

Quoting directly from the book "MVC 2 in action":
When you called Trace.Write() in Web
Forms, you were interacting with the
Trace- Context class. This exists on
your ViewPage in ASP.NET MVC, but this
isn’t where you would want to write
tracing statements. By the time you’ve
passed the baton over to the view,
there’s no logic there that you’d need
to trace. Instead, you’d like to trace
the logic embedded in your
controllers. You might try to leverage
the TraceContext class in your
controller, but these statements won’t
ever make their way to the list of
messages in the trace log (on your
page or on Trace.axd). Instead, you
can use System.Diagnostics.Trace and
set up your own TraceListeners to
inspect the activity in your
controllers. Alternatively, you can
leverage a more mature logging
framework such as log4net or NLog:
You debug ASP.NET MVC applications
just as you would any .NET
application. Tracing, however, doesn’t
offer as much for MVC. Instead, you
can lean on the built-in
TraceListeners in .NET, or utilize a
good logging library like those
mentioned earlier. Another aspect of
error logging is health monitoring.
Sorry for not answering in my own words, but I think this explanation is spot on :)

Actually, now this can be done. Using glimpse, you can return and use Trace.Write in MVC.
http://getglimpse.com/
Only for ASP.NET MVC 3.0 and up

Instead of using the HttpContext.Current.Trace or the System.Diagnostics.Trace, try using the Controller.HttpContext.Trace ?

The quote doesn't apply to tracing in filters and actions. Even then, it doesn't apply as you can get trace output from OnResultExecuting. It's only OnResultExecuted where it's too late to get trace output.
I was only able to get integrated trace output to work in IIS 7.5 by running my MVC 3 application in Cassini - then integrated tracing started working in IIS 7.5.
I'm investigating some trace anomolies in MVC

You could try to use Html.Action to get the controller to do the tracing for you.
For example, if you need to trace the error from the default Error view, you can use
Html.Action("TraceError", "Error", new { ControllerName = Model.ControllerName, ActionName = Model.ActionName, Exception = Model.Exception })
Then inside your Error Controller, implement the method
public ActionResult TraceError(String ControllerName, String ActionName, Exception Exception)
{
System.Diagnostics.Trace.TraceError("Error Message: {0}", Exception.Message);
// Other tracing statements
}

Related

#Html.DeleteConfirmation in a Plugin View (Admin) in asp.net core 2.0

Looking at core code examples of say... Edit Action for SpecificationAttribute. I am looking to replicate this delete function in the same manner in my plugin, however, getting an error.
Getting this error
'IHtmlHelper' does not contain a definition for 'DeleteConfirmation' and no extension method 'DeleteConfirmation' accepting a first argument of type 'IHtmlHelper' could be found (are you missing a using directive or an assembly reference?)
I have used in 3.90 but it getting error in 4.0 asp.net core Has anyone used #Html.DeleteConfirmation in a plugin (on administration side) that could give me a clues
I have a span element in the html like the following;
<span id="myattribute-delete" class="k-button">#T("Admin.Common.Delete")</span>
And trying to use #Html.DeleteConfirmation("myattribute-delete")
I know I probably need to double check my route, by the error is leading me to think the issue is not a routing issue.
As #Stephen stated it's not part of MVC, and it's custom helper by nopCommerce.
#Html.DeleteConfirmation is helper till/in nopCommerce 3.90, but in 4.0 it has been changed to
<nop-delete-confirmation asp-model-id="#Model.Id" asp-button-id="myattribute-delete" />
And that helper located at Nop.Web.Framework.TagHelpers.Adminso you have to add reference of it to your view file.

MVC.NET Tracing Partials & Templates

Is there a way to get a trace of what cshtml files are being used to construct a particular page load? For instance:
/Views/Shared/_LayoutUser.cshtml
/Views/Shared/_UserNavigation.cshtml
/Views/Shared/_BannerSelector.cshtml
RenderBody - /Views/Users/Index.cshtml
/Views/Users/DisplayTemplate/User.cshtml
Anything else used to construct view
/Views/Shared/_UserFooter.cshtml
Try Glimpse. It works much like Firebug does, except for MVC. It shows the execution tree of your views/partials/layout in a tree that looks pretty much the same as you described, including their execution times. It's very extensible as well, and there are plugins to trace Entity Framework, and JavaScript, etc.

Configuring NHibernate second level caching in an MVC app

I have an MVC3 app that's using NHibernate. All was going well until I started to try and add second level caching. After browsing the web for a few hours I finally found what I think is the right dll (NHibernate.Caches.SysCache2.dll) and have added it to my project.
However, I can't find any help for configuring it with an MVC app. All the examples refer to having an App.config file (I presume this is for Web Forms). I'm a Java developer who's learning .Net so I'm not so familiar with how to rig everything up.
Could someone point me in the right direction. I'm using xml hibernate (hbm) files.
Thanks.
I'm using 2nd level caching with MVC3 and NHibernate.Caches.SysCache.dll like this...
1st, add a config section to your web.config file like this
<configSections>
<section name="syscache" type="NHibernate.Caches.SysCache.SysCacheSectionHandler, NHibernate.Caches.SysCache, Version=3.0.0.4000, Culture=neutral, PublicKeyToken=6876f2ea66c9f443"/>
</configSections>
add a syscache section to your web.config under your configuration section like this:
<syscache>
<cache region="SomeCustomNameRegion" expiration="86400" priority="5" />
</syscache>
in my hibernate.cfg.xml file i have the following properties added:
<property name="cache.provider_class">NHibernate.Caches.SysCache.SysCacheProvider, NHibernate.Caches.SysCache</property>
<property name="cache.use_query_cache">true</property>
<property name="cache.use_second_level_cache">true</property>
I'm using FluentNhibernate to create my mappings and add this to support caching:
public CustomerClassMap()
{
Cache.NonStrictReadWrite();
Id(x => x.Id);
Map(x => x.Name);
//... more properties
}
And then in my data access code, I have something similar to this for queries that I want cached:
public IEnumerable<Customer> GetAllCached()
{
return Session.CreateCriteria(typeof(Customer))
.SetCacheable(true)
.SetCacheRegion("SomeCustomNameRegion")
.SetCacheMode(CacheMode.Normal)
.AddOrder(Order.Desc("CreateDate"))
.List<Customer>();
}
Here are some helpful links to get into a little more detail. There's nothing specific about MVC3 that you need to worry about, and most is unchanged from earlier versions of NHibernate as far as i can tell.
http://www.klopfenstein.net/lorenz.aspx/using-syscache-as-secondary-cache-in-nhibernate
http://blog.symbiotic-development.com/2008/02/27/more-configuring-nhibernate-caches/

T4MVC renderpartial and Spark views

#Html.RenderPartial(MVC.Shared.Views._Sorter);
throws error =>
There is no build provider registered for the extension '.spark'. You can register one in the section in machine.config or web.config. Make sure is has a BuildProviderAppliesToAttribute attribute which includes the value 'Web' or 'All'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Web.HttpException: There is no build provider registered for the extension '.spark'. You can register one in the section in machine.config or web.config. Make sure is has a BuildProviderAppliesToAttribute attribute which includes the value 'Web' or 'All'.
Any ideas what might be wrong?
Adding this to web.config=>system.web=>compilation
<buildProviders>
<add extension=".spark" type="System.Web.Compilation.PageBuildProvider" />
</buildProviders>
Forces app to build spark view as webforms view and produces ${Something.something} all around. So - what's an equivalent for spark viewengine?
Can you please try to make the following change to T4MVT.tt. In the method ViewsFolderInfo.AddView, change the line:
Views[viewFieldName] = GetVirtualPath(item);
to
Views[viewFieldName] = viewFieldName;
If that works well, we can just add a switch to the settings file that makes it do this instead of the default full path.
Let me know how that goes!
Note that MVC.Shared.Views._Sorter is just a constant with the path to the view. If you were to write this code without T4MVC, what exact string would you pass in there?
Maybe Spark has different requirements in the type of view paths it accepts here? If needed, we can tweak T4MVC to make this work, but I'd like to fully understand the Spark behavior outside of T4MVC first.

Spark View Engine "An assembly with the same simple name" error

I am trying out the Spark view engine with ASP MVC but get the error below when displaying a view. The view references model objects that are in a different assembly to the main web application (MyApp.Model.dll). I am stuck on the cause or resolution to this.
Dynamic view compilation failed.
0,0): error CS1704: An assembly with the same simple name
'MyApp.Model, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null has already been imported. Try removing one of the
references or sign them to enable side-by-side.
It sounds like you're referencing the same assembly multiple times in your view. Instead of using the <use assembly=""> element you might want to use the <use namesace=""> element
Craig I got the same problem, if I modify some class in assembly X and do a build, I got this error at runtime for the assembly X.
But after a "rebuild" (not "build") it's working.
For me it has been in the middle of a project. It comes maybe from an update ?
Matthieu

Resources