# Build Guide - .NET PCMonitorAgent Project

## 📋 Overview

PCMonitorAgent adalah Windows Service yang ditulis dengan .NET 6.0. Project ini harus di-build sebelum bisa diinstall.

---

## 🔧 Prerequisites

1. **.NET 6.0 SDK** atau lebih tinggi
2. **Command Prompt/PowerShell**
3. **Administrator access** (untuk publish)

### Verifikasi .NET terinstall:
```bash
dotnet --version
```

Harus return version `6.0.x` atau lebih tinggi.

Jika belum install:
→ Download dari: https://dotnet.microsoft.com/download

---

## 📁 Project Structure

```
agent-csharp/
├── PCMonitorAgent.csproj      (Project file)
├── Program.cs                  (Main entry point)
├── Models/                     (Data models)
├── Services/                   (Business logic)
│   ├── ActivityMonitor.cs
│   ├── ProcessMonitor.cs
│   ├── BandwidthController.cs
│   ├── CommandExecutor.cs
│   └── SystemInfoMonitor.cs
├── src/
│   ├── AgentService.cs
│   ├── ApiClient.cs
│   ├── ConfigReader.cs
│   └── ServiceWrapper.cs
├── Installers/
│   └── ServiceInstaller.cs
├── config.json                (Configuration)
└── INSTALL_SIMPLE.bat         (Installer script)
```

---

## 🚀 Build Steps

### Step 1: Open Command Prompt/PowerShell

```bash
# Navigate to project
cd E:\xampp\htdocs\tracker\agent-csharp
```

### Step 2: Restore NuGet Packages

```bash
dotnet restore
```

**What it does:**
- Download semua NuGet packages dari `PCMonitorAgent.csproj`
- Packages yang dibutuhkan:
  - `Newtonsoft.Json` (JSON processing)
  - `Serilog` (Logging)
  - `System.ServiceProcess.ServiceController` (Windows Service)

**Expected output:**
```
Restore complete in XX.XX sec
```

### Step 3: Build Project

```bash
dotnet build -c Release -f net6.0-windows
```

**Flags explained:**
- `-c Release` = Release configuration (optimized, production)
- `-f net6.0-windows` = Target .NET 6.0 Windows only

**Expected output:**
```
Build succeeded.
    0 Warning(s)
    0 Error(s)
```

### Step 4: Publish for Distribution

```bash
dotnet publish -c Release -f net6.0-windows -o bin
```

**What it does:**
- Package executable dengan semua dependencies
- Output ke folder `bin`
- Ready untuk di-copy ke PC lain

**Output akan ada di:**
```
E:\xampp\htdocs\tracker\agent-csharp\bin\Release\net6.0-windows\
```

---

## ✅ Verify Build Success

Cek apakah executable ada:

```bash
# PowerShell
dir bin\Release\net6.0-windows\PCMonitorAgent.exe

# CMD
dir bin\Release\net6.0-windows\PCMonitorAgent.exe
```

File harus ada dengan size > 1MB.

---

## 📝 One-Line Build Command

Copy-paste ini untuk build semua sekaligus:

```bash
cd E:\xampp\htdocs\tracker\agent-csharp && dotnet restore && dotnet build -c Release -f net6.0-windows && dotnet publish -c Release -f net6.0-windows -o bin && dir bin\Release\net6.0-windows\PCMonitorAgent.exe
```

---

## 🔍 Build Options

### Development Build (untuk testing)
```bash
dotnet build -c Debug
```
- Lebih cepat build
- Lebih besar file size
- Include debug symbols
- Lebih lambat runtime

### Release Build (untuk production) ⭐ RECOMMENDED
```bash
dotnet build -c Release
```
- Lebih lambat build
- Lebih kecil file size
- Optimized
- Lebih cepat runtime

### Self-Contained Publish
```bash
dotnet publish -c Release -f net6.0-windows --self-contained -o bin
```
- Include .NET runtime
- Tidak perlu .NET install di PC target
- Lebih besar file size (~100MB)

---

## 🛠️ Troubleshooting

### ❌ Error: "dotnet not recognized"

**Cause:** .NET SDK tidak terinstall atau tidak di PATH

**Fix:**
```bash
# Check if installed
dotnet --version

# If not, install from: https://dotnet.microsoft.com/download
```

### ❌ Error: "No SDK found"

**Cause:** .NET 6.0 tidak terinstall, mungkin hanya .NET 7/8 yang ada

**Fix:**
```bash
# Check installed versions
dotnet --list-sdks

# Install .NET 6.0: https://dotnet.microsoft.com/download/dotnet/6.0
```

### ❌ Error: "Unable to restore packages"

**Cause:** NuGet server error atau internet connection

**Fix:**
```bash
# Clear cache
dotnet nuget locals all --clear

# Try restore again
dotnet restore

# Or specify NuGet source
dotnet restore --source https://api.nuget.org/v3/index.json
```

### ❌ Error: "Build failed - CS1234"

**Cause:** Compilation error dalam source code

**Fix:**
```bash
# Check error message
dotnet build -c Release -v detailed

# Fix source code errors dan rebuild
```

### ❌ Error: "publish output directory already exists"

**Cause:** Folder `bin` sudah ada

**Fix:**
```bash
# Delete existing output
rmdir /s /q bin

# Publish again
dotnet publish -c Release -f net6.0-windows -o bin
```

---

## 📦 Output Files

Setelah publish sukses, di folder `bin\Release\net6.0-windows\`:

```
PCMonitorAgent/
├── PCMonitorAgent.exe              (Main executable) ⭐
├── PCMonitorAgent.dll
├── PCMonitorAgent.runtimeconfig.json
├── Newtonsoft.Json.dll
├── Serilog.dll
├── Serilog.Sinks.File.dll
├── System.ServiceProcess.ServiceController.dll
└── [Other dependencies]
```

---

## 🔐 Installation After Build

Setelah build sukses:

```bash
# 1. Verify executable exists
dir bin\Release\net6.0-windows\PCMonitorAgent.exe

# 2. Run installer (as Administrator)
INSTALL_SIMPLE.bat

# 3. Or manual setup
cd E:\xampp\htdocs\tracker\agent-csharp
INSTALL_SIMPLE.bat
```

---

## 📊 Build Size References

| Build Type | Size | Build Time |
|-----------|------|-----------|
| Debug | ~15 MB | 10sec |
| Release | ~8 MB | 15sec |
| Self-Contained Release | ~110 MB | 20sec |

---

## ⏱️ Typical Build Timeline

```
dotnet restore     → 10-20 seconds (first time), 1-2 seconds (cached)
dotnet build       → 5-15 seconds
dotnet publish     → 5-10 seconds
Total              → ~20-40 seconds first time, ~10-20 seconds after
```

---

## 💡 Tips

1. **First build lebih lambat** - NuGet packages perlu di-download
2. **Caching works** - Build kedua lebih cepat
3. **Use Release untuk production** - Optimized dan lebih cepat
4. **Keep bin folder** - Jangan delete, tinggal re-publish

---

## 🎯 Next Step

Setelah build sukses, jalankan installer:

```bash
INSTALL_SIMPLE.bat
```

Lihat guide di `PATH_FIX.md` untuk detail instalasi.

---

**Last Updated:** 2026-03-27  
**Target Framework:** .NET 6.0-windows  
**Output Type:** Windows Desktop Application
