In this post I'll show how to create a logging provider that writes logs to the file system. In production, I'd recommended using a more fully-featured system like Serilog instead of this library, but I wanted to see what was involved to get a better idea of the process myself.
The code for the file logging provider is available on GitHub, or as the NetEscapades.Extensions.Logging.RollingFile package on NuGet.
12.Visual Studio 2017 15.9 منتشر شد
Issues Fixed in 15.9.12
These are the customer-reported issues addressed in 15.9.12:
- Access violation C++ /CLI 15.9.5 ISO C++ Latest Draft Standard since 15.9.5.
- An error occurred loading this property page (CSS & JSON).
- Visual Studio 2017 crashing when editing package.json.
- Opening package.json locks up Visual Studio.
- PGO Code Gen Bug - Vectorized instruction accessing memory OOB.
- Bad code gen in recursive bucket split routine.
- Compiler optimization bug in 15.8.9.
- We have fixed a linker error LNK4020 when using PCH, /Zi, and /GL in distributed build systems, such as IncrediBuild. The C++ compiler backend now correctly associates CIL OBJs with their corresponding compiler generated PDB when generating debug info for cross-module inlining.
Security Advisory Notices
As Microsoft launches .NET 7, CODE Focus offers high quality insights right from the teams responsible for designing and improving the product. Dig into articles about C# 11, .NET MAUI, Blazor, EF Core 7, CoreWCF and better tools to upgrade your existing .NET and ASP.NET applications to the latest release. Plus performance enhancements everywhere! This is an amazing release.
.NET applications however rely on the ciphers provided by the OS, and the only way to get new ciphers into the OS is through a patch from Microsoft. Unsupported versions of Windows typically do not receive these patches, so over time you can expect an increasing number of websites to stop working with .NET applications.
کارگاه آموزشی الگوهای جاوااسکریپت
Impersonation Process
Impersonation is when an admin user is logged in with the same privileges as a user, but without knowing their password or other credentials. I’ve used this in couple applications and it was invaluable for support cases and debugging user permissions.
[Authorize(Roles = "Admin")] // <-- Make sure only admins can access this public async Task<IActionResult> ImpersonateUser(String userId) { var currentUserId = User.GetUserId(); var impersonatedUser = await _userManager.FindByIdAsync(userId); var userPrincipal = await _signInManager.CreateUserPrincipalAsync(impersonatedUser); userPrincipal.Identities.First().AddClaim(new Claim("OriginalUserId", currentUserId)); userPrincipal.Identities.First().AddClaim(new Claim("IsImpersonating", "true")); // sign out the current user await _signInManager.SignOutAsync(); await HttpContext.Authentication.SignInAsync(cookieOptions.ApplicationCookieAuthenticationScheme, userPrincipal); return RedirectToAction("Index", "Home"); }
این نگارش بر روی سیستم من مشکلی نداشت ولی پس از چند گزارش عدم امکان اجرای آن بر روی سایر سیستمها، یک ماشین مجازی ویندوز 8.1 را تهیه و برنامه را بر روی آن اجرا کردم. بله ... برنامه هنوز به مرحلهی نمایش نرسیده، محو میشد. در این مرحلهی ابتدایی امکان تهیهی لاگ استثنای حاصل توسط برنامه وجود نداشت و تنها این خطا در event viewer ویندوز (Computer management -> event viewer -> windows logs -> application) قابل مشاهده بود:
Application: DNTProfiler.exe Framework Version: v4.0.30319 Description: The process was terminated due to an unhandled exception. Exception Info: System.Windows.Markup.XamlParseException Stack: at System.Windows.Markup.WpfXamlLoader.Load
روش حل این نوع مشکلات و بررسی آنها در خارج از VS.NET، با استفاده از برنامهی معروف WinDBG مایکروسافت میسر است.
دو نگارش 32 بیتی و 64 بیتی آنرا از اینجا میتوانید دریافت کنید:
http://codemachine.com/downloads.html
پس از دریافت، بهتر است نسخهی 32 بیتی آنرا اجرا کنید، زیرا برای دیباگ برنامههای دات نت این نسخه است که امکان بارگذاری فایل C:\Windows\Microsoft.NET\Framework\v4.0.30319\sos.dll را دارد.
تا اینجا فرض بر این است که نسخهی 32 بیتی windbg را دریافت و اجرا کردهاید. در ادامه از منوی File آن گزینهی Open executable را انتخاب کرده و فایل exe برنامه را به آن معرفی کنید.
CommandLine: C:\Tests\DNTProfiler.1.0.808.0\DNTProfiler.exe Symbol search path is: srv* Executable search path is: ModLoad: 00150000 00176000 DNTProfiler.exe ModLoad: 76f30000 77098000 ntdll.dll ModLoad: 72ee0000 72f36000 C:\Windows\SysWOW64\MSCOREE.DLL ModLoad: 74be0000 74d20000 C:\Windows\SysWOW64\KERNEL32.dll ModLoad: 756e0000 757af000 C:\Windows\SysWOW64\KERNELBASE.dll (684.764): Break instruction exception - code 80000003 (first chance) eax=00000000 ebx=00000000 ecx=37af0000 edx=00000000 esi=7eb8f000 edi=00000000 eip=76fe2d15 esp=002ff604 ebp=002ff630 iopl=0 nv up ei pl zr na pe nc cs=0023 ss=002b ds=002b es=002b fs=0053 gs=002b efl=00000246 *** ERROR: Symbol file could not be found. Defaulted to export symbols for ntdll.dll - ntdll!LdrResolveDelayLoadsFromDll+0xa89: 76fe2d15 cc int 3
0:000> g ModLoad: 757b0000 75827000 C:\Windows\SysWOW64\ADVAPI32.dll ModLoad: 05080000 05102000 Newtonsoft.Json.dll (684.764): C++ EH exception - code e06d7363 (first chance) (684.764): C++ EH exception - code e06d7363 (first chance) (684.764): C++ EH exception - code e06d7363 (first chance) (684.764): CLR exception - code e0434352 (first chance) First chance exceptions are reported before any exception handling. This exception may be expected and handled. eax=002fd0a0 ebx=00000005 ecx=00000005 edx=00000000 esi=002fd164 edi=00000001 eip=756f3d67 esp=002fd0a0 ebp=002fd0f8 iopl=0 nv up ei pl nz ac pe nc cs=0023 ss=002b ds=002b es=002b fs=0053 gs=002b efl=00200216 *** ERROR: Symbol file could not be found. Defaulted to export symbols for C:\Windows\SysWOW64\KERNELBASE.dll - KERNELBASE!RaiseException+0x48: 756f3d67 8b4c2454 mov ecx,dword ptr [esp+54h] ss:002b:002fd0f4=3fce1188
0:000> sxe clr 0:000> g (684.764): C++ EH exception - code e06d7363 (first chance) (684.764): CLR exception - code e0434352 (first chance) First chance exceptions are reported before any exception handling. This exception may be expected and handled. eax=002fe2a0 ebx=00000005 ecx=00000005 edx=00000000 esi=002fe368 edi=00000001 eip=756f3d67 esp=002fe2a0 ebp=002fe2fc iopl=0 nv up ei pl nz ac pe nc cs=0023 ss=002b ds=002b es=002b fs=0053 gs=002b efl=00200216 KERNELBASE!RaiseException+0x48: 756f3d67 8b4c2454 mov ecx,dword ptr [esp+54h] ss:002b:002fe2f4=3fce2388
اکنون میخواهیم پیام استثنای واقعی دات نت را مشاهده کنیم. به همین جهت ابتدا دستور loadby sos clr! و سپس دستور CLRStack! را صادر میکنیم:
0:000> !loadby sos clr 0:000> !CLRStack *** ERROR: Symbol file could not be found. Defaulted to export symbols for C:\Windows\Microsoft.NET\Framework\v4.0.30319\clr.dll - PDB symbol for clr.dll not loaded OS Thread Id: 0x764 (0) Child SP IP Call Site 002fe3f4 756f3d67 [GCFrame: 002fe3f4] 002fe430 756f3d67 [GCFrame: 002fe430] 002fe528 756f3d67 [GCFrame: 002fe528] 002fe544 756f3d67 [HelperMethodFrame_2OBJ: 002fe544] System.RuntimeTypeHandle.CreateInstance(System.RuntimeType, Boolean, Boolean, Boolean ByRef, System.RuntimeMethodHandleInternal ByRef, Boolean ByRef) DNTProfiler.App.Main() 002ff190 720e2652 [GCFrame: 002ff190]
0:000> !PrintException Exception object: 021e6f0c Exception type: System.Reflection.TargetInvocationException Message: Exception has been thrown by the target of an invocation. InnerException: System.IO.FileNotFoundException, Use !PrintException 021e6950 to see more. StackTrace (generated): <none> StackTraceString: <none> HResult: 80131604 0:000> !PrintException 021e6950 Exception object: 021e6950 Exception type: System.IO.FileNotFoundException Message: Could not load file or assembly 'System.Net.Http.Formatting, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified. InnerException: <none> StackTrace (generated): SP IP Function 00000000 00000001 DNTProfiler!DNTProfiler.Services.SelfHostConfig.getHostConfiguration(System.String, Boolean)+0x2 002FE298 006B4E54 DNTProfiler!DNTProfiler.Services.SelfHostConfig.OpenWait(System.String, Boolean)+0x14 002FE2B0 006B4A3F DNTProfiler!DNTProfiler.ViewModels.MainWindowViewModel.doStart(System.String)+0x1f 002FE2C0 006B368D DNTProfiler!DNTProfiler.ViewModels.MainWindowViewModel..ctor(DNTProfiler.Common.Mvvm.ICommonDialogsService, DNTProfiler.Common.Controls.DialogManagement.Contracts.IDialogManager)+0xcd 002FE2D8 006B04C9 DNTProfiler!DNTProfiler.MainWindow..ctor()+0x91 StackTraceString: <none> HResult: 80070002
روش حل این مشکل، مراجعه به خواص پروژه، یافتن اسمبلی System.Net.Http.Formatting در لیست ارجاعات برنامه و سپس true کردن خاصیت copy to local آن است. به این ترتیب این اسمبلی در کنار فایل اجرایی برنامه کپی خواهد شد.
بنابراین خلاصهی عملیات یافتن علت اصلی کرش برنامه در خارج از ویژوال استودیو به صورت ذیل است:
الف) اجرای نسخهی 32 بیتی برنامهی windbg
ب) انتخاب منوی File آن و سپس باز کردن فایل اجرایی برنامه توسط گزینهی Open executable
ج) اجرای دستورات ذیل به ترتیب:
0:000> g 0:000> sxe clr 0:000> g 0:000> !loadby sos clr 0:000> !CLRStack 0:000> !PrintException
چند نمونهی مشابه برای مطالعهی بیشتر
Finding CLR exceptions without visual studio
Power of WinDBG: The story of mysterious crash of WPF application