5. Given $f(x)=\left(x^{2}+3 x+2\right)^{\cos \pi x}$. Then the sum of all $n$ that satisfy the equation
$$
\left|\sum_{k=1}^{n} \log _{10} f(k)\right|=1
$$

is
5. 21 .

It is known that for integer $x$, we have
$$
f(x)=[(x+1)(x+2)]^{(-1)^{x}} \text {. }
$$

Thus, when $n$ is odd,
$$
\sum_{k=1}^{n} \log _{10} f(k)=-\log _{10} 2-\log _{10}(n+2) \text {; }
$$

When $n$ is even,
$$
\sum_{k=1}^{n} \log _{10} f(k)=-\log _{10} 2+\log _{10}(n+2) \text {. }
$$

Therefore, the $n$ that satisfies the condition is 3 or 18, and their sum is 21.