Back To Home

Name: Ngày 28: Sitecore Logs & Diagnostics

📝 Ngày 28: Sitecore Logs & Diagnostics

Debug trong Sitecore gần như 100% dựa vào logs.
Nếu bạn hiểu cách hoạt động log và các công cụ diagnostics, bạn sẽ xử lý lỗi nhanh hơn rất nhiều.


🔍 1. Các loại Log trong Sitecore

Sitecore sinh ra nhiều loại log khác nhau nằm trong:

 
/Website/App_Data/logs/

1.1 Sitecore Log (quan trọng nhất)

Tên file dạng:

 
log.YYYYMMDD.HHMMSS.txt

Chứa:

  • Runtime errors

  • Rendering errors

  • Database error

  • Pipeline lỗi

  • Crawler/Indexing warnings

  • Performance warnings


1.2 Crawling / Index Logs

 
Crawler.log Search.Crawling.log

Dùng khi debug:

  • Không index item

  • Search trả sai kết quả


1.3 Publishing Logs

 
publish.log

Dùng để kiểm tra:

  • Publish failed

  • Publish không chạy

  • Publish performance issues


1.4 Diagnostic Logs (Azure setup)

Trong môi trường Azure:

 
/App_Data/logs/diagnostic/

1.5 xConnect Logs

Nếu dùng XP hoặc Marketing Automation:

  • xconnect-search.txt

  • xconnect-collection.txt

  • xconnect-tracking.txt

Debug:

  • Analytics

  • Contact/Interaction tracking


📌 2. Cách đọc Log hiệu quả

💡 Tìm các từ khóa sau:

  • ERROR

  • FATAL

  • EXCEPTION

  • WARN

  • InnerException

  • Stack Trace

Ví dụ stack trace:

 
Exception: System.NullReferenceException Message: Object reference not set to an instance of an object.

⚡ 3. Tăng mức độ log (Debug Mode)

Mặc định Sitecore log ở mức INFO.

Bạn có thể bật DEBUG mode để ghi log chi tiết hơn:

Mở file:

 
/App_Config/Sitecore.config

Tìm:

 
<setting name="Diagnostics.LogLevel" value="INFO" />

Đổi thành:

 
<setting name="Diagnostics.LogLevel" value="DEBUG" />

⚠ Chỉ bật debug trong môi trường DEV, không bật ở PROD vì:

  • Log quá nặng

  • Ảnh hưởng performance


🧪 4. Log trong code C# (rất thường dùng)

Sử dụng Sitecore.Diagnostics:

 
Sitecore.Diagnostics.Log.Info("Check payment response", this); Sitecore.Diagnostics.Log.Warn("Payment slow > 5s", this); Sitecore.Diagnostics.Log.Error("Payment failed", ex, this);

Ghi log có context:

 
Log.Info($"Order {orderId} starting payment", this);

Ghi log lỗi:

 
try { ProcessPayment(); } catch (Exception ex) { Log.Error("ProcessPayment exception", ex, this); }

🛠 5. /sitecore/admin trang hỗ trợ Diagnostics

Sitecore có nhiều trang admin giúp debug nhanh:


🧰 5.1 /sitecore/admin/showconfig.aspx

Xem toàn bộ cấu hình sau khi merge file config + patch.


🧰 5.2 /sitecore/admin/stats.aspx

Xem:

  • CPU usage

  • Memory usage

  • Cache usage


🧰 5.3 /sitecore/admin/cache.aspx

Xem và xóa cache:

  • Data Cache

  • Item Cache

  • HTML Cache

  • Media Cache


🧰 5.4 /sitecore/admin/index.aspx

Quản lý index (Solr)


🧰 5.5 /sitecore/admin/serialization.aspx

Kiểm tra serialization


🧰 5.6 /sitecore/admin/ShowServicesConfig.aspx

Debug các dịch vụ


🧩 6. Debug theo Rendering

Muốn debug component nào, bạn nên:

  1. Bật trình tự pipeline rendering

  2. Log tại Controller / View Rendering

Ví dụ:

 
Log.Debug($"[ProductList] Datasource: {item.Paths.FullPath}", this);

🚨 7. Common Errors trong log & cách xử lý

❌ NullReferenceException

→ Thiếu datasource, thiếu item, sai field name.

❌ SqlException

→ DB connection lỗi, query timeout.

❌ Could not find layout

→ Sai đường dẫn layout hoặc thiếu file view.

❌ The model item passed into the dictionary is of type…

→ Model binding MVC sai type.

❌ Media request error

→ File media bị hỏng hoặc không publish.


🧾 8. Log Viewer Tool (rất quan trọng)

Sitecore có desktop Log Viewer:

 
/sitecore/shell/client/applications/logviewer/

Hoặc bạn có thể dùng:

✔ Sitecore Log Analyzer

https://github.com/Sitecore/Sitecore-Log-Analyzer

Tính năng:

  • Highlight lỗi

  • Group theo loại exception

  • Filter theo thời gian

  • Tìm stack trace


🎯 Bài tập ngày 28

✔ Bài 1

Tìm log mới nhất trong folder:

 
App_Data/logs/

✔ Bài 2

Tạo lỗi NullReference trong controller và xem log xuất hiện thế nào.

✔ Bài 3

Bật DEBUG LogLevel trong dev → kiểm tra log chi tiết.

✔ Bài 4

Dùng Log Analyzer để đọc log 1 ngày.

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