简体   繁体   English

如何查找大型.NET应用程序中未使用的部分?

[英]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. 考虑一个大型的多层企业Web应用程序和许多功能非常复杂的服务,这些服务大多数是在服务器端用.NET(C#)编写的,在客户端显然是html和javascript的,它们由数百个页面组成,其中包含服务调用的数量(行动)数量众多,托管在多台服务器上,并经过15年的发展。 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? 假设调试符号可用,是否可以在实时C#.NET应用程序中测量覆盖率(类似于执行的代码行)?

You can use ReSharper. 您可以使用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". 在菜单中,您将找到条目“ ReSharper>检查>解决方案中的代码问题”。 It will create a report, there you will find it under "Redundancies in Code". 它将创建一个报告,您可以在“代码冗余”下找到该报告。

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. 我将从从(IIS?)服务器收集所有日志文件开始(至少一年,每年可以使用一次代码)并进行分析。 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. 检查带有时间指示的内部表和loginfo以获取最新条目。

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. 监视数据库:可以使用事件探查器工具,但是创建触发器以记录所有CRUD操作以及所需的所有信息可能更容易。 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. 监视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 . 在这里看看: 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. 创建一个程序来过滤有关url,状态,ip,标识等的日志,以确定所使用的内容。

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. 只是一些想法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM