Tracking All Code Usage In ASP.NET Web Application - c#

I have been tasked with taking an existing asp.net website that has many lines of code and projects and redesigning it. I would like to know if anyone has any ideas on how to track every method/property that gets called when users are on the site. I would like to identify the code used the most so that I can know what I should carry over to the redesign, and what code is not used at all and can potentially be removed completely. Many thanks in advanced. There are both vb.net and c# projects in the solution, so any solution would have to support each language. Also, any free/oss solutions are the best for me right now.
I am currently using VS.NET 2015 Community, if that helps. :-)

You can do it in multiple ways:
Introduce some method level tracing by using some AOP framework (like PostSharp). In this case, you can log the method call chain of one specific request. Then you can start from there.
If your app backed with SQL Server, enable profiling and look through all SQL queries executed from one request. Then you look back to codebase and refactor them.
Use CodeMap to understand code base and do it.

You can use ReSharper. Right click the project and select 'Find Code Issues'. You will see 'Unused Symbols' to show which components are not being used.

Related

How to find unused parts of a large .NET application?

Consider a large multi-tier enterprise web application and many services with very complex functionality, mostly written in .NET (C#) on the server side and obviously html and javascript on the client, consisting of many hundred pages with the amount of service calls (actions) well in the thousands, hosted on multiple servers and developed over 15 years. Some parts are very new and modern, other parts are legacy.
Some parts of this application are obsolete and nobody actually uses those parts anymore. Whether these are whole unused sub-applications, unused pages, files, service calls, methods or even lines of code, doesn't matter. Older parts do not provide any usage statistics but do use dependency injection.
How can one automatically find out, based on access to production servers, which parts are unused, without changing the actual source code? So the question is not finding unreferenced / unreachable code. It's about finding parts that users don't actually use anymore.
One option could be looking at query logs. This discovers unused pages, but it is very difficult (a tedious manual process) to find out which parts in the background are used by those pages only.
Another option could probably be monitoring file access on servers. Does that make sense? Would that be feasible?
Yet another thought is doing something like test coverage tools do, but not during testing. Could coverage (something like lines of code executed) be measured in a live C#.NET application, assuming that debug symbols are available?
It is hard to give an answer without really knowing the situation. However, I do not think there is some automatic or easy way. I do not know the best solution, but I can tell you what I would do. I would start with collecting all log files from the (IIS?) server (for at least a year, code could be used once a year) and analyze those. This should give you the best insight on which parts are called externally. You do have those logs?
Also check the eventlogs. Sometimes there are messages like 'Directory does not exist', which could mean that the service isn't working for years but nobody noticed. And check for redundant applications, perhaps applications are active on multiple servers.
Check inside tables with time indications and loginfo for recent entries.
Checking the dates on files and analyzing the database may provide additional information, but I don't think it will really help.
Make a list of all applications that you think are obsolete, based on user input or applications that should be obsolete.
Use your findings to create a list based on the probability that application /code is obsolete. Next steps, based on your list, could be:
remove redundant applications.
look for changes in the datamodel of filesystem and check if these still match with the code.
analyze the database for invalid queries. This could indicate that the datamodel has changed, causing the application to stop working. If nobody noticed then this application or functionality is obsolete.
add logging to the code where you have doubts.
look at application level and start with marking calls as obsolete, comment / removing unused code or redirect to (new) equivalent code.
turn off applications and monitor what happens. If there is a dependency then you can take action to remove this dependency or choose to let the application live.
Monitoring the impact of your actions will help you to sort things out. I hope this answer gives you some ideas.
-- UPDATE --
There may be logging available, but collecting, reading and interpreting may be hard and time consuming. To make it easier to monitor you could think of the following:
monitor database: you can use the profiler tool, but it may be easier to create a trigger that logs all CRUD operations with all the information you need. Create a program that can read the scheme of the database and filter the log by table, stored procedure, view to determine what isn't used. I didn't investigate, but perhaps you can monitor rollbacks and exceptions as well.
monitor IIS. There are off course the log files, but you can also think of adding a Module to the website where you can write custom code to monitor whatever you want. All traffic passes the module. Take a look here: https://www.iis.net/learn/develop/runtime-extensibility/developing-iis-modules-and-handlers-with-the-net-framework. If I am not mistaken all you have to do is add the module to the website and configure the website to use the module. Create a program to filter the log on url, status, ip, identification, etc. to determine what is used.
I think that is sufficient for first analysis. It then comes to interpreting the logs. Perhaps you'll see a way to combine the logs so you can link a request to certain database actions, without having to look in or change the code. Just some thoughts.
You can use ReSharper. It will tell you such problems while you're coding.
However you can also detect problems afterwards. In the Menu you will find the entry "ReSharper > Inspect > Code Issues in Solution".
It will create a report, there you will find it under "Redundancies in Code".

Separating existing web project

I have inherited an existing .Net/angularJS project. We have a need moving forward to allow customization per client, while still maintaining synchronization through version control (in this case, git).
I'm not a .Net developer--my .Net experience is limited to writing a service a couple of years ago, starting the BrowserStack tests for the project, and the occasional foray for code review type activities. I'm primarily a Flash/Flex developer with a fair amount of ASP Classic and some PHP experience.
My preliminary research seems to indicate that I can do what I need to do with a git subtree, but I need to find where the seams should be to separate out the custom stuff from the shared code. Right now, the HTML and JS are in the same directory as the web services. My first order of business will be to separate those out, but I don't completely understand
Why everything's all in one place to begin with
What the implications are of moving things (project settings, paths, etc.)
When I wrote the service way back, I do remember that we had to scrap the service because the server we had the site on didn't support that version of .Net and it wouldn't work across domains so I could host the service on a server where it would work. I know that things have changed and there's now a way to allow that, but I figure that's the sort of problem I should be looking to avoid as I do this.
I figure I can't be the first person needing to make this kind of separation in a project I think started from the monolithic web project template, but because of a short deadline and a lack of knowledge of .Net, I'd feel better if someone could point me in the right direction or at least alert me to some of the gotchas I should plan to encounter.
Are you Trying to decouple the Projects. If so than this might be a good help.
http://www.codeproject.com/Articles/439688/Creating-ASP-NET-application-with-n-tier-architect
One of my recent project was almost the same that you mentioned above, So I ended up scrapping the old version and Create a brand new Project and Decoupled the related stuffs in the solution.
The best way of understanding stuff is to make sure you seperate the Client Side (Javascript/Htmls/CSS) and Server Side (EF/SP Calls/DTOs etc) by creating different project to the same solution.
Hope this Helps.
So I kept digging, and I finally found a pair of tutorials that address exactly this issue.
Creating an empty ASP.Net Project Powered by Angular JS Using Visual Studio
AngularJS Consuming ASP.NET Web API RESTful Services
In a nutshell, you copy the client's url from the properties panel to the service properties panel and add '/api' to the end of the URL and allow VS to create a virtual directory for you.
Now for my next trick, figuring out how to publish it...

Organising webparts in visual studio 2010

Quick question. When creating web parts in vs2010, is it best to group all web parts into one project or create separate projects. I cant quite get my head round how best to organise this. The web parts will be part of a bigger Intranet solution but will be completely separate entities that will be developed and updated independently from each other over a period of time.
Quick question, not so quick answer. It indeed depends on the structure of your intranet and how you want to make it available.
Here are a few thinks that are important to consider.
Are a lot of the webparts going to share code (ex. I can imagine logging will be shared)? Ok you can put the
shared code in a separate project, but if you are going to reuse a
lot it can be handy to have everything in the same solution. If you are sure that you want them separate, you can go with the wsp for each webpart solution. But consider that you will also have a separate feature for each webpart.
Where are your webparts being used? If you have a few webapplications and only certain webparts are going to be used in a certain webapplication. Then you can consider putting those webparts in a separate package.
Hand in hand with the previous topic, do you always want to make every webpart available? If you work with one webapplication, with different sitecollections it is possible that you don't always want to so all the webparts. To solve this you can consider putting the webparts under different features and only activating the feature of the webparts that you want.
I am sure there are some more, but these are the ones that come to mind.
actually its depends on your requirement. but its fine if wrap all in the single as only webpart executes which we adds on the page. so its not going to affect any performance issue or anything else.

Easy update assistance

We are writing simple architectured software in C# 3.5.
What I was searching for is for easy updating framework/application...
We don't have COM components, Servicdes to install or whatever, so basically what we need is xcopy stuff form one Server side directory to the client. So what the program should do is like a workflow:
Check some location \Server\Updates for manifest.xml
Check the local setup verison
Download all available DLLs (doesn't necessary all need for complete application, so kind of patch stuff) and substitude them with "old" ones.
I'm aware of ClickOnce, of App publishing, also our setup generator supports autoamtic updates (even if noone yet used it, and I don't want to use to not strongly couple ourselfs with that technology).
Do you know some app or technology, that I'm not aware (actually listed in question) which could better fit our needs. ?
Thank you in advance.
So, after the comment, the answer:
I would look at this SO post.
I looked wyWuild and the AutomaticUpdater controler some time ago. It really looked great.
EDIT
I just remembered that I chose to use AppLife Update in the end. It's more expensive but the features are extremely good

.NET - Database deployments similar to Rails

I have a .NET application that uses a database. I am familiar with how Rails handles database deployments where there is a set of scripts that have an 'UP' and 'DOWN' method with a set of SQL in each to deploy/rollback changes to your database. The rake command will then execute these scripts in order.
I was interested in a similar process for my .NET application and was hoping there was something similar already written. I don't think it would be hard to write my own to do this process, but I was hoping to avoid re-inventing the wheel.
Thanks for any direction.
Try Migrator.Net if what you want is rails-like migrations with simple up/down methods for adding/removing tables, columns, indexes, keys etc, and written in C#/VB etc.
There is the Tarantino project, that aims to solve this problem.
Look at Visual Studio Database project
Visual studio supports a database project type that has folders that can contain "Post-Deployment" and "Pre-Deployment" scripts to run. MSDeploy may then be used to execute these scripts (as well as other DDL) during the deployment process. Here is the documentation on MSDN regarding the use of Pre- and Post-Deployment script definition.
Here at Red Gate, we're currently building in a migrations feature into SQL Compare and SQL Source Control. If you're interested and you'd like more information on how this will work, please email me at David dot Atkinson at red-gate dot com. We're keen to get some feedback on our ideas.

Categories