Back To Home

Name: Ngày 26: Debug code Sitecore trong Visual Studio

🐞 Ngày 26: Debug code Sitecore trong Visual Studio

Debug trong Sitecore quan trọng hơn nhiều so với các hệ thống .NET bình thường, vì Sitecore có pipeline phức tạp, caching, config patching, và nhiều module chạy nền.
Hôm nay bạn sẽ học cách debug chuẩn trong môi trường CM/CD, bao gồm pipeline, controller, rendering, scheduled tasks, và xConnect.


1️⃣ Chuẩn bị Visual Studio để Debug Sitecore

✔ Build chế độ Debug

Trong Visual Studio:

 
Build → Configuration Manager → Active solution configurationDebug

✔ Attach symbol (.pdb)

Khi build, Visual Studio tạo file .pdb.
Đảm bảo thư mục website (CM/CD) dùng đúng file build từ solution của bạn.


2️⃣ Attach Debugger vào IIS

Sitecore chạy dưới w3wp.exe, nên để debug:

🔹 Bước 1: Run Visual Studio as Administrator

Nếu không → attach sẽ bị blocked.

🔹 Bước 2: Debug → Attach to Process

Chọn:

 
w3wp.exe

Nếu có nhiều app pool:

  • CM dùng: sc.dev.local

  • CD dùng: sc.cd.dev.local

  • Identity dùng: identityserver.dev.local

🔹 Bật "Show processes from all users"


3️⃣ Debug Controller Rendering

Đặt breakpoint trong:

 
/Controllers/... /Models/... /Services/...

Mở trang có controller rendering → reload → Visual Studio sẽ bắt breakpoint.

❗ Tip: Nếu không bắt breakpoint

Chắc chắn 1 trong các lỗi:

  • Code chưa build vào đúng folder webroot

  • Symbol load chưa đúng (.pdb mismatch)

  • Attaching sai w3wp instance

  • App pool đang chạy dưới chế độ 32-bit nhưng bạn build 64-bit hoặc ngược lại

  • Debugger không load symbol
    Kiểm tra trong VS:
    Debug → Windows → Modules → search your DLL → Load Symbols


4️⃣ Debug View Rendering

View Rendering không có controller, nhưng bạn vẫn debug logic thông qua:

  • Model Builders

  • Glass Mapper

  • Pipeline RenderField

  • Custom HTML Helpers

Đặt breakpoint ở:

 
/Models/ViewModels/... /Mapping/... /GlassMapper Config/...

Ngoài ra, trong .cshtml bạn có thể debug bằng:

 
@{ System.Diagnostics.Debugger.Break(); }

⚠️ Chỉ dùng ở local — KHÔNG bao giờ commit dòng này.


5️⃣ Debug Pipeline Processor

Sitecore pipelines chạy trong nhiều thời điểm: Render Layout, MVC Routing, Authentication, Indexing.

Vị trí:

 
App_Config/Include/*.config

Chỉ cần đặt breakpoint trong processor:

 
public class CustomPipelineProcessor { public void Process(RenderLayoutArgs args) { var id = args.Layout; // đặt breakpoint tại đây } }

Reload trang → Visual Studio sẽ bắt vào.


6️⃣ Debug Scheduled Task

Sitecore Scheduled Task chạy qua:

 
/sitecore/admin/Jobs.aspx

Hoặc bạn có thể chạy thủ công:

 
/sitecore/admin/RunTask.aspx?name=YOUR_TASK_NAME

Breakpoint trong class task runner sẽ hoạt động bình thường.


7️⃣ Debug Indexing (Solr)

Đặt breakpoint trong:

 
ComputedIndexField Crawler IIndexConfiguration

Chạy lệnh reindex:

 
/sitecore/admin/ControlPanel.aspx → Indexing → Rebuild

Debugger sẽ bắt.


8️⃣ Debug Rendering Parameters

Trong Controller Rendering:

 
var parameters = RenderingContext.Current.Rendering.Parameters;

Đặt breakpoint → load page.


9️⃣ Debug Personalization

Nếu logic personalization không chạy, kiểm tra:

✔ Debug trong pipeline preprocessRequest

Breakpoint trong:

 
Sitecore.Pipelines.HttpRequest

✔ Debug Condition logic

Đặt breakpoint trong custom rule:

 
public class CustomRule : RuleAction<RuleContext>

Load page → customize rule triggered.


🔟 Debug xConnect

Nếu bạn làm Sitecore XP:

  • Attach debugger vào xconnect service:

 
w3wp.exe (xConnect)
  • Breakpoint trong:

 
Facet Interaction Contact creation Tracker Current Context

1️⃣1️⃣ Debug Performance Issue (trễ, khó chịu)

✔ Debug cache

Trong Visual Studio Immediate Window:

 
Sitecore.Caching.CacheManager.GetAllCaches()

✔ Debug slow pipeline bằng Stopwatch

 
var sw = Stopwatch.StartNew(); ... sw.Stop(); Log.Info($"XPipeline took {sw.ElapsedMilliseconds} ms", this);

1️⃣2️⃣ Công cụ hỗ trợ Debug

🔹 Sitecore Log Viewer

Xem realtime logs.

🔹 Glimpse (cho MVC pipeline)

Dùng để xem:

  • Pipeline

  • View rendering

  • Layout resolution

🔹 JetBrains dotTrace / dotMemory

Debug hiệu năng và memory leak.


🧪 Bài tập Ngày 26

Bài 1

Tạo một Controller Rendering → đặt breakpoint → attach debugger → reload trang → bắt lỗi.

Bài 2

Viết 1 pipeline processor → đặt breakpoint → chạy thử.

Bài 3

Debug Glass Mapper model binding bằng cách đặt breakpoint trong class mapping.

Donald Trump

Để trở thành người chiến thắng, bạn cần biết khi nào là đủ. Đôi khi trong cuộc sống, bạn phải từ bỏ cuộc chiến và chuyển sang mục tiêu mới mang lại hiệu quả hơn

Related Post